Simply put. Why did this make my code malfunction after awhile.
//Color[][] colorArr = new Color[Width][Height]();
private void shiftRowsDown(int row) {
for (int i = row; i > 0; i--)
{
colorArr[i] = colorArr[i - 1];//<--This in particular
}
for (int col = 0; col < colorArr[0].length; col++)
{
colorArr[0][col] = null;
}
}
while changing it to manually change one by one was fine.
private void shiftRowsDown(int row) {
for (int i = row; i > 0; i--) {
for(int col = 0;col < colorArr[i].length;col++)
{
colorArr[i][col] = colorArr[i - 1][col];//<--This in particular
}
}
for (int col = 0; col < colorArr[0].length; col++)
{
colorArr[0][col] = null;
}
}
You have an array of arrays, so your first code sets two elements of the outer array to the same inner array.
Simpler example:
So you can visualize colors’ memory like:
If you then do:
it is:
The expanded structure is now:
But both rows are references to the same array, at the same memory position. If you then do:
the array of arrays is still:
and the expanded structure now looks like:
This is also called a shallow copy.