Davey
Davey

Reputation: 101

Converting ArrayLists to arrays

I wanted to know if it was possible to change

ArrayList<ArrayList<String>> 

to String[][] in java. I do not think that the toArray() function would recurse inside its generic parameter. Any advice would be greatly appreciated.

Upvotes: 3

Views: 214

Answers (2)

Snicolas
Snicolas

Reputation: 38168

if bigL is your bigger array list, and tab has been created to bigL.size().

Can't you do something like :

for( ArrayList<String> l : bigL )
{
  tab[ index ++ ] = l.toArray( new String[ l.size() ] );
}//for

Upvotes: 0

Tom Anderson
Tom Anderson

Reputation: 47163

You're right that toArray won't recurse. I'm afraid you'll have to do this manually. Something like:

List<List<String>> stringLists;
String[][] stringArrays = new String[stringLists.size()][];
int i = 0;
foreach (List<String> stringList: stringLists) {
    stringArrays[i] = stringList.toArray(new String[stringList.size()]);
    ++i;
}

I haven't actually tried that, mind, so it could be rubbish.

Upvotes: 3

Related Questions