Final array is being changed

Some of my clone() examples I did a long time ago at the link below may help ya out: :smile_cat:

final int[][] stuff = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

final int[] myArray = stuff[2]; // it's an alias for stuff[2].
myArray[0] = 10; // thus it alters stuff[2] too!
println(stuff[2][0]); // prints out 10. stuff[2] is also affected!

final int[] myClone = stuff[2].clone(); // it's a clone for stuff[2].
myClone[0] = 20; // thus it's independent from stuff[2]!
println(stuff[2][0]); // still prints out previous 10. stuff[2] isn't affected!

exit();
final int[][] stuff = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
for (final int[] thing : stuff)  println(str(thing));
println();

final int[][] myClone = stuff.clone(); // Cloning the outer dimension of the 2D array.
for (final int[] clone : myClone)  println(str(clone));

myClone[0][0] = 100;
print('\n', stuff[0][0]); // Prints out 100. It's still not a real clone!

// In order to fix that, we gotta clone() each of its inner arrays too:
for (int i = 0; i != myClone.length; myClone[i] = stuff[i++].clone());

myClone[0][0] = 200;
println('\n', stuff[0][0]); // Still prints out previous 100 and not 200.
// It's a full clone now and not a mere reference alias!

exit();
1 Like