I'm trying to add Arrays of integer to an ArrayList but it seems that each time that a new Array is beeing added, the content of all the others is modified to be the same as the last.
for (int a = 0; a<taille; a++) {
temp[0] = this.caseX-a;
temp[1] = this.caseY;
this.occupe.add(temp);
for ( int j = 0; j < this.occupe.size(); j++ )
System.out.println("element " + j + ": " + this.occupe.get(j)[0] );
The caseX
and caseY
are two integers and my array is this.occupe
.
The output is :
element 0: 4
element 0: 3
element 1: 3
element 0: 2
element 1: 2
element 2: 2
element 0: 1
element 1: 1
element 2: 1
element 3: 1
While it should be :
element 0: 4
element 0: 4
element 1: 3
element 0: 4
element 1: 3
element 2: 2
element 0: 4
element 1: 3
element 2: 2
element 3: 1