1

In my project I have a List that contains Lists of Strings (List<List<String>>) and now I have to convert this List into an array of String arrays (String[][]).

If I had a single List<String> (for example myList) then I could do this to convert to String[]:

String[] myArray = new String[myList.size()];
myArray = myList.toArray(myArray);

But in this case I have lots of List of String inside a List (of List of Strings). How can I do this? I've spent lots of time but I didn't find a solution..

2
  • 3
    Questions are supposed to show some effort, so I suggest taking what you consider to be your best effort and posting some information about what you tried and what went wrong. Commented Aug 23, 2013 at 16:17
  • Ok thanks.. But I really didn't know how to do that.. Commented Aug 23, 2013 at 17:44

4 Answers 4

10

I wrote a generic utility method few days back for my own usage, which converts a List<List<T>> to T[][]. Here's the method. You can use it for your purpose:

public static <T> T[][] multiListToArray(final List<List<T>> listOfList, 
                                         final Class<T> classz) {
    final T[][] array = (T[][]) Array.newInstance(classz, listOfList.size(), 0);

    for (int i = 0; i < listOfList.size(); i++) {
        array[i] = listOfList.get(i).toArray((T[]) Array.newInstance(classz, listOfList.get(i).size()));
    }

    return array;
}

There I've created the array using Array#newInstance() method, since you cannot create an array of type parameter T directly.

Since we're creating a 2-D array, and we don't yet know how many columns you will be having, I've given the column size as 0 initially. Anyways, the code is initializing each row with a new array inside the for loop. So, you don't need to worry about that.

This line:

array[i] = listOfList.get(i).toArray((T[]) Array.newInstance(classz, listOfList.get(i).size()));

uses the List#toArray(T[]) method to convert the inner list to an array, as you already did. I'm just passing an array as a parameter to the method so that the return value which I get is T[], which I can directly assign to the current row - array[i].

Now you can use this method as:

String[][] arr = multiListToArray(yourList, String.class);

Thanks to @arshaji, you can modify the generic method to pass the uninitialized array as 2nd parameter, instead of Class<T>:

public static <T> void multiListToArray(final List<List<T>> listOfList, 
                                        final T[][] arr) {
    for (int i = 0; i < listOfList.size(); ++i) {
        arr[i] = listOfList.get(i).toArray(arr[i]);
    }
}

But then, you have to pass the array to this method like this:

List<List<String>> list = new ArrayList<>();
// Initialize list
String[][] arr = new String[list.size()][0];
multiListToArray(list, arr);

Note that, since now we are passing the array as argument, you no longer need to return it from your method. The modification done in the array will be reflected to the array in the calling code.

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

6 Comments

When I saw the question, I remember your code review. Very nice answer (method).
+1 Although if I were writing this, I would pass an array to be filled instead of a Class instance to the method.
@arshajii. Nice idea. I'll try out that, and post it too, if get it to work. Thanks :)
@arshajii. That will still require me to pass Class<T> as parameter. That will be needed to create the inner arrays, which is of type T.
You shouldn't have to. array is the parameter and, in the for-loop, array[i] = listOfList.get(i).toArray(array[i]).
|
5
 String[][] myArray  = new String[myList.size()][];
 for (int i = 0; i < myList.size(); i++) {
         List<String> row = myList.get(i);
         myArray[i] = row.toArray(new String[row.size()]);
 }

1 Comment

I would just change ArrayList<String> to List<String>.
2

Here is one way

List<List<String>> list = new ArrayList<List<String>>();
String[][] array = new String[list.size()][];

int counter1 = 0;

for(List<String> outterList : list)
{
    array[counter1] = new String[outterList.size()];
    int counter2 = 0;

    for(String s : outterList)
    {
        array[counter1][counter2] = s;
        counter2++;
    }

    counter1++;
}

1 Comment

oops, people don't like the traditional way :)
-1

To init String[][], you should init the list(array) of String[]

So you need to define a list

List<String[]>  string = new List<String[]>();
for (int i = 0;....)
{
String[] _str = new String[numbers];
_str[0] = ...
string.append(_str);
}

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.