As Mark said, you are accessing initialCoordinates
with the coordinates
variable because you assigned it by reference. This means that after coordinates [i]=initialCoordinates;
, coordinates[i]
will then reference the same memory address that initalCoordinates
does so that when one changes the other changes also.
What you probably wanted to do is copy the values which would have the effect of assigning it by value. You can do that using System.arraycopy (See this answer). By assigning by value, you allocate a separate chunk of memory to hold the values for coordinates[i]
which will be copied from initialCoordinates
rather than having both variables pointing to the same thing in memory.
int [] initialCoordinates = {26,0};
int [] positions={1,2,3,4};
int [][] coordinates = {{0,0},{0,0},{0,0},{0,0}};
for(int i=0;i<4;i++){
System.out.println("Initial: "+initialCoordinates[1]);
System.arraycopy(initialCoordinates, 0, coordinates[i], 0, initialCoordinates.length);
coordinates [i][1]+=positions[i];
}