*
The moose likes Java in General and the fly likes convert ArrayList of String[] to String[][] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "convert ArrayList of String[] to String[][]" Watch "convert ArrayList of String[] to String[][]" New topic
Author

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

Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: May 22, 2003
Posts: 657

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

Joined: Jan 29, 2003
Posts: 8791
Ah, that makes passing the right size sound worth while!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert ArrayList of String[] to String[][]
 
Similar Threads
need help sorting a string array alphabetically
casting of an ArrayList
how to serialize String[ ][ ]
Batches of array in an ArrayList
Array declaration