• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

multidimensional array

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 42148
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this, but isn't it to heavy treatment ?
 
Campbell Ritchie
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic