aspose file tools*
The moose likes Beginning Java and the fly likes multidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multidimensional array" Watch "multidimensional array" New topic
Author

multidimensional array

dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello i would like to know how to convert an ArrayList containing other ArrayList to a two dimensional array Object[][] ?
Thank you for help.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Dav,
You could write a nested loop. The outer loop would go through the outer ArrayList and the inner loop would go through the inner ArrayList.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello and thank you very much for your answer.
I keep trying though and i find one way that seems to be working, tell me what you think about it compare to other solution like nested loop :

[ April 18, 2008: Message edited by: dav mrazek ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Suggest you lose the second size

Object[][] myArray = new Object[array.size()][];

That way each individual member ArrayList can set its own size. Remember there is no such thing as a multidimensional array in Java, only arrays of arrays.
I don't think you need the Object[] cast, but I am not sure.

Otherwise it looks very good to me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Note that the ArrayList class is generic (since Java 5), so it is worth comparing its details and the return type of the toArray() method in Java 1.4.2 and Java 6.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello and thank you for answers,
Now i would like to have my array of array converted back to an arraylist of arraylist.
What is the right way of doing it ?

Thank you.
[ April 21, 2008: Message edited by: dav mrazek ]
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
I found this, but isn't it to heavy treatment ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Looks all right to me. You can simplify it slightly by using a for-each loop (=enhanced for), but there is not a lot else which will simplify your loop.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multidimensional array
 
Similar Threads
reading a user chosen file into array
Naming Objects
size of the list
Date difference, with the table containing null values for date
String array to arraylist