• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multidimensional array

 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34656
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this, but isn't it to heavy treatment ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic