2

I have a 2D arraylist like the following

   ArrayList<ArrayList<String>> childrenSuperList = new ArrayList<ArrayList<String>>();
    ...
    childrenList = new ArrayList<String>();
    ...
    childrenSuperList.add(childrenList);

How can I convert it to a simple string array like

String s[][]= {{"a","b","c"},
{"d","e","f"},
{"a","b","c"}
}

5 Answers 5

4
final String[][] r = new String[childrenSuperList.size()][];
int i = 0;
for (ArrayList<String> l : childrenSuperList) 
  r[i++] = l.toArray(new String[l.size()]);
Sign up to request clarification or add additional context in comments.

5 Comments

Why don't you use for(int i = 0; i < n; i++) { loop? It seems you try to simulate this loop with for each loop.
@NikitaBeloglazov Simulate? I iterate over the source ArrayList and additionally keep track of the index into the destination array. This is the shortest and cleanest code. Why would I want to use the ugly childrenSuperList.get(i) instead?
I just feel wrong about index, that escaped loop and accessible outside of the loop. In for (int i loop it will be more obvious that we set i-th element of list to i-th element of the array. Yes it is more verbose, but I think in some ways it's clearer.
@NikitaBeloglazov The choice is an ugly method call + an ugly for definition against an i that escapes the loop scope. Whichever you choose, it's not 100% the way you or I would like it. I just happen to like the second option better, especially if those three lines are the only three lines of a conversion method.
childrenSuperList.get(i) has poor performance, especially with linked lists. This answer uses the built in iterator, which is better.
2

Your list can have different sizes so table wont have fixed sizes, but that is no problem.

ArrayList<ArrayList<String>> childrenSuperList = new ArrayList<ArrayList<String>>();
// put some data
ArrayList<String> a = new ArrayList<>();
a.add("a1");
a.add("a2");
ArrayList<String> b = new ArrayList<>();
b.add("b1");
b.add("b2");
b.add("b3");

childrenSuperList.add(a);
childrenSuperList.add(b);

// you need to know sizes of array
String[][] array = new String[childrenSuperList.size()][];
int i = 0, j = 0;
for (ArrayList<String> row : childrenSuperList) {
    array[i] = new String[row.size()];
    j = 0;
    for (String str : row) {
        array[i][j] = str;
        j++;
    }
    i++;
}
System.out.println(Arrays.deepToString(array));

2 Comments

When may it throw NullPointerException? I know that childrenSuperList is initialized. Even if list is empty it will be ok.
I think he's saying childrenSuperList might contain null elements, which is a little extreme IMO. Your answer seems fine to me.
0

There is a method to have string array from ArrayList:

ArrayList listArray = new ArrayList();

listArray.add("One");
listArray.add("Two");
listArray.add("Three");

String []strArray = new String[3];
listArray.toArray(strArray);

Comments

0

The below code will help you to get the perfect complete result.

public static void main(String[] args) {

    ArrayList<ArrayList<String>> val = new ArrayList<ArrayList<String>>();
    int arraySize = 0;
    int upperSize = 0;

    if (val != null) {
        ArrayList<String> arrayList = val.get(0);
        upperSize = val.size();
        if (arrayList != null) {
            arraySize = arrayList.size();
        }
    }

    int i = 0, j = 0;
    String[][] values = new String[upperSize][arraySize];
    if (val != null) {
        for (ArrayList<String> curr : val) {
            if (curr != null) {
                for (String currValue : curr) {
                    values[i][j++] = currValue;
                }
                i++;
            }
        }
    }

}

3 Comments

This assumes all inner lists are of the same size, which may not be true.
@Paul In the question it is not clearly mention regarding that point, hence I made that.
@BhavikAmbani That seems like a pretty huge assumption, that's almost certainly going to turn out to be wrong. It strikes me as silly to write code that hinges on that being true when it's possible to solve the problem without doing so.
0
import java.util.*;

public class HelloWorld{

 public static void main(String []args){

     ArrayList<ArrayList<String>> parentList = new ArrayList<ArrayList<String>>();

    ArrayList<String> list=new ArrayList<String>();
    list.add("Joshan Stethem");
    list.add("Bruce Wills");
    list.add("Tom Cruse");
    list.add("Optimus Prime");
    System.out.println("Array List Hollywood : "+list);
    ArrayList<String> list1=new ArrayList<String>();
    list1.add("Salman Khan");
    list1.add("Shahrukh Khan");
    list1.add("AMir Khan");
    list1.add("Fawad Khan");   
     System.out.println("Array List Bollywood : "+list1);
       ArrayList<String> list2=new ArrayList<String>();
    list2.add("Rahat Fateh");
    list2.add("Arijit Singh");
    list2.add("KK");
    list2.add("Ankit Tiwari");   
     System.out.println("Array List Singers : "+list2);
    parentList.add(list);
    parentList.add(list1);
    parentList.add(list2);

 String[][] twodimArray = new String[parentList.size()][];
     int i = 0, j = 0;
 for (ArrayList<String> row : parentList) {
  twodimArray[i] = new String[row.size()];
   j = 0;
   for (String str : row) {
        twodimArray[i][j] = str;
         j++;
        }
       i++;
      }
 System.out.println("List of Artist : "+Arrays.deepToString(twodimArray));
     }

}

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.