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
---------------------