aspose file tools*
The moose likes Java in General and the fly likes Creating a 2d square matrix from a 2d integer arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a 2d square matrix from a 2d integer arraylist" Watch "Creating a 2d square matrix from a 2d integer arraylist" New topic
Author

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

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

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


Learning and Learning!-- Java all the way!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
Please look for methods which can turn Lists into arrays. You might find them in the java.util.Collections class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a 2d square matrix from a 2d integer arraylist
 
Similar Threads
need help with calculation in my spreadsheet array
Manipulating Vectors (code not working)
Write a method that can be called, that will initialize the seating plan.
ArrayLists ;o(
Multidimensional ArrayList