Owen Kosnen
Owen Kosnen

Reputation: 75

How to convert 2Darray into 2D ArrayList in java?

I have searched similar questions and I still cannot find the solution for my case. So basically, I have a 2D array called 'array2D' and I am trying to convert those into 2D arrayList. I tried to create an arrayList and used for loop to copy each of the elements from my array2D, which is a primitive 2D Array. I wonder if there is any clean and efficient way to do it.

Here is my code:

    List<List<String>> arrayList2D = new ArrayList<List<String>>();
    List<String> eachRecord = new ArrayList<String>();

    for (int i=0; i< array2D.length; i++){
      for(int j =0; j<array2D[1].length; j++){
        eachRecord.add(String.valueOf(array2D[j]));
      }
      arrayList2D.add(eachRecord);
      eachRecord.clear();
    }

    System.out.println(Arrays.toString(arrayList2D.toArray()));

The result returns empty arrayList, which means I failed to convert them.

[[], [], [], [], [], [], [], [], [], [], [], [], [], []]

I suspect the reason why I failed to populate the new ArrayList is because of the wrong usage of add method, I have tried push method as well and it was compile error. I wonder if there is any clean and efficient way to do it.

Upvotes: 0

Views: 3549

Answers (3)

kstandell
kstandell

Reputation: 785

You can convert your 2D array using the Arrays#asList method

// Initialize the array to the correct size :)
List<List<String>> arrayList2D = new ArrayList<>(array2D.length);

for (String[] record : array2D) {
    arrayList2D.add(Arrays.asList(record));
}

Upvotes: 1

Uma Kanth
Uma Kanth

Reputation: 5629

I would suggest 3 improvements in your code.

  1. use array2D[i].length instead of array2D[1].length.

  2. use eachRecord.add(String.valueOf(array2D[i][j])); instead of eachRecord.add(String.valueOf(array2D[j]));.

array2D[index] returns a total array. array2D[indexRow][indexCol] returns the object at those indexes.

  1. Instead of clear() initiate the eachRecord list inside the loop.

List<String> eachRecord = new ArrayList<String>(); inside the first for loop.

String [][]array2D = {{"A", "B"}, {"C", "D"}, {"E", "F"}};
    List<List<String>> arrayList2D = new ArrayList<List<String>>();
    for (int i = 0; i < array2D.length; i++) {
        List<String> eachRecord = new ArrayList<String>();
        for (int j = 0; j < array2D[i].length; j++) {
            eachRecord.add(String.valueOf(array2D[i][j]));
        }
        arrayList2D.add(eachRecord);
    }
    System.out.println(arrayList2D);//[[A, B], [C, D], [E, F]]

If you want to add whole array you could use Arrays#asList method.

String[][] array2D = { { "A", "B" }, { "C", "D" }, { "E", "F" } };
    List<List<String>> arrayList2D = new ArrayList<List<String>>();
    for (int i = 0; i < array2D.length; i++) {
        List<String> eachRecord = Arrays.asList(array2D[i]);
        arrayList2D.add(eachRecord);
    }
    System.out.println(arrayList2D);

Upvotes: 3

alexandrum
alexandrum

Reputation: 427

The reason for the empty result is that the lineeachRecord.clear(); removes the references from your result list arrayList2D. A simple solution here could be to replace that line with eachRecord = new ArrayList<String>();

Also change j<array2D[1] with j<array2D[i] in order to work properly.

Upvotes: 2

Related Questions