3

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

you get it, 3 secs before me :)
I got lucky - my typing is usually so bad it takes me another minute to get my posts fixed before i can post them, but today it came out quite quickly!
I was gonna ask how you typed and formatted it so fast ;) Do you usually use eclipse to answer ? I type directly in the textfield and correct indentation manually.
No, i just type in the text area. I am a pretty fast typist, but i'm generally quite inaccurate, so although i can type something fast, i then need to spend as much time fixing it as i spent typing it. This time, i didn't make as many mistakes as usual.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.