Win a copy of Mesos in Action this week in the Cloud/Virtualizaton 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
Pie
Posts: 48964
60
  • 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
Pie
Posts: 48964
60
  • 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