• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

convert ArrayList of String[] to String[][]

 
Author
Posts: 144
5
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can create a ArrayList and convert to String[]. How do I convert an ArrayList of String[] to String [][]?



Thanks,
Tom
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same way.

stringArrayArray = (String[][]) stringArrayArrayList.toArray(new String[stringArrayArrayList.size()][]);

When you allocate an array of arrays, you only have to specify the first dimension, as here.
[ September 01, 2005: Message edited by: Ernest Friedman-Hill ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you can specify a size of 0. The List only cares about the types, not the size. I don't know if it's worth saving a few cycles to allocate a new list just for that argument.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
I don't know if it's worth saving a few cycles to allocate a new list just for that argument.



Yeah. But both IDEA and Eclipse have a keyboard shortcut to automatically do out the whole thing, including allocating the right-sized array, and I've seen it so often that to me, at least, it feels like an idiom.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And you can specify a size of 0. The List only cares about the types, not the size.

There's more to it than just type. According to the contract of toArray(), it will use the array you pass, if the array is large enough.
[ September 01, 2005: Message edited by: Steve Morrow ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that makes passing the right size sound worth while!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic