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.

Owen Kosnen
  • 75
  • 2
  • 6

3 Answers3

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);
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • 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. – Owen Kosnen Jan 16 '17 at 09:16
  • `List#add()` expects an object. not an array. You could use [this](http://stackoverflow.com/questions/12853595/how-to-add-elements-of-a-string-array-to-a-string-array-list) to insert all the elements to an `arrayList`. – Uma Kanth Jan 16 '17 at 09:18
  • Your suggestion no.3 really helps. Thanks. But I am still wondering why initiating the eachRecord inside the loop make difference though. – Owen Kosnen Jan 16 '17 at 09:23
  • @Owen, Alex answer explains why it is empty. – Uma Kanth Jan 16 '17 at 09:24
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.

alexandrum
  • 429
  • 9
  • 17
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));
}
kstandell
  • 775
  • 6
  • 16
  • Obviously if you are using 1.6 or below then you will not be able to use the <> operator when assigning the `ArrayList`. – kstandell Jan 16 '17 at 09:31