This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!See this thread for details.
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Creating a 2d square matrix from a 2d integer arraylist

Ranch Hand
Posts: 477
• Number of slices to send:
Optional 'thank-you' note:
Hello Folks,

Ive written the following code that converts a 2d arraylist into a 2d matrix, but now I need to check that the matrix created is also square(num of rows = num of columns).

Could anyone explain how I could possibly modify my code to acheive that?

My code is

Marshal
Posts: 78706
374
• Number of slices to send:
Optional 'thank-you' note:
What’s a 2D list?     Is there such a thing? I think there isn’t, only a List of Lists.
What’s a 2D array? Is there such a thing? I think there isn’t, only an array of arrays.

That may appear pedantic, but arrays of arrays are better than 2D arrays. For example, the outermost array has a length field, which you doubtless already know about. And the element arrays also have a length field. So you can iterate the outer array with a for‑each loop and check that all the lengths are the same.
You can do the same sort of thing with the size() method of your Lists. It might be quicker to do that validation at as early a stage as possible.

Campbell Ritchie
Marshal
Posts: 78706
374
• Number of slices to send:
Optional 'thank-you' note:
Please look for methods which can turn Lists into arrays. You might find them in the java.util.Collections class.

 I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com