| Author |
convert ArrayList of String[] to String[][]
|
Tom Henricksen
Ranch Hand
Joined: Mar 23, 2004
Posts: 135
|
|
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: 24040
|
|
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: 24040
|
|
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!
|
 |
 |
|
|
subject: convert ArrayList of String[] to String[][]
|
|
|