Hi Everyone,

I'm fairly new to Java, and am having problems in trying to convert an ArrayList containing String arrays to an array of String arrays.
I thought it would be relatively simple to do, but I keep running into problems. I've looked at all the examples I've been able to find through Google that appear to pertain to my problem, but am no further forward:sad:.
I can obtain an array of objects (each object within the array being an array of strings) using:

Object[] myObjectArray = my2DArListData.toArray();

where my2DArListData is my ArrayList of arrays of strings.

But how can I convert this into an array of arrays of strings? Or is there a better way that avoids the above step?
Any suggestions will be very gratefully received - I've been looking into this for hours!


Edited by Dani: Fixed formatting

6 Years
Discussion Span
Last Post by NormR1

An approach:
Create a two dim array of String with the first array defined by the size of the Arraylist.
Iterate thru the array list and set each second dim of the String array to next element of the ArrayList


That's it!

Thanks Norm, the following works perfectly:

    String[][] ar2DStringArray = new String[my2DArListData.size()][];

    for(int i = 0; i < my2DArListData.size(); i ++)
        ar2DStringArray[i] = my2DArListData.get(i);

I had previously tried something similar but I was trying to declare the array using:

String[][] ar2DStringArray = new String[1][my2DArListData.size()];

Or variations of it. Argghh! I really feel stupid!

Thanks again for sorting that out, and for the quick reply!



Edited by Dani: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.