1

I'm trying to do an ArrayList of 2 dimensional arrays. It is the same 2 dim array that I'm adding to the ArrayList, but it has different values each time. The problem is : when I add the array to the list, it's auto-updating the other versions of the array in the list. I tried to clone/copy the array just before adding it to the List, but it has no effect.

import java.util.*;

public class Test {
   static ArrayList<int[][]> list = new ArrayList<int[][]>();

   public static void main(String[] args) {
      Lister L = new Lister();
   }

   public static void add(int[][] array) {
      list.add(array);
      printArray();
   }

   public static void printArray() {
      for (int i = 0; i < list.size(); i++) {
         System.out.println("Element: " + i);
         printDim(list.get(i));
      }
      System.out.println("--------------------------------");
   }

   public static void printDim(int[][] array) {
      for (int x = 0; x < array.length; x++) {
         for (int y = 0; y < array[0].length; y++) {
            System.out.print(array[y][x]+" ");
         }
         System.out.println();
      }
      System.out.println("-----------");
   }
}

class Lister {
   Lister() {
      int[][] array1 = new int[5][5];
      array1[0][4] = 1;
      Test.add(array1);
      int[][] array2 = array1.clone();
      array2[1][2] = 1;
      Test.add(array2);
   }
}

Output:

Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
---------------------
Element: 0
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
-----------
Element: 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
--------------------

Expected Output:

Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
--------------------
Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
-----------
Element: 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
---------------------
1

2 Answers 2

1

A 2D array cannot be shallow copied. Since it's an array of arrays, a shallow copy will give you a new outer array holding references to the same inner arrays as the original.

You need to implement a deep copy instead:

int[][] array2 = array1.clone();
for (int i = 0; i < array2.length; i++) {
    array2[i] = array1[i].clone();
}

Please not that this only works for primitive arrays. If you have object arrays, you need to copy each object as well (except you're fine with having the same objects referenced).

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

1 Comment

Thank you! I knew it was because of weird pointers but didn't know how to solve it. Thank you again
1

It's because you're still using the same 'matrix' of type int. This should be fixed by declaring it again after using the first one.

import java.util.*;

class Test {
   static ArrayList<int[][]> list = new ArrayList<int[][]>();

   public static void main(String[] args) {
      int[][] array1 = new int[5][5];
      array1[0][4] = 1;
      list.add(array1);

      array1 = new int[5][5];
      array1[1][2] = 1;

      list.add(array1);
      printArray();
   }

   public static void printArray() {
      for (int i = 0; i < list.size(); i++) {
         printDim(list.get(i));
      }
   }

   public static void printDim(int[][] array) {
      for (int x = 0; x < array.length; x++) {
         for (int y = 0; y < array[0].length; y++) {
            System.out.print(array[y][x]+" ");
         }
         System.out.println();
      }
      System.out.println("--------");
   }
}

1 Comment

Thank you for your answer, but the problem is that I want for each added arrays to have the value of the previous one..

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.