This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look for methods which can turn Lists into arrays. You might find them in the java.util.Collections class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic