0

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.

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I forgot to mention that I do not intend to copy the first element and want to directly jump starting from array2D[1][0]. As for your suggestion no.2, It still does not work and return empty arraylist.
List#add() expects an object. not an array. You could use this to insert all the elements to an arrayList.
Your suggestion no.3 really helps. Thanks. But I am still wondering why initiating the eachRecord inside the loop make difference though.
@Owen, Alex answer explains why it is empty.
2

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.

Comments

1

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));
}

1 Comment

Obviously if you are using 1.6 or below then you will not be able to use the <> operator when assigning the ArrayList.

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.