Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tom Henricksen
Author
Ranch Hand
Posts: 144
5
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that makes passing the right size sound worth while!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic