# Final array is being changed

**URL:** https://discourse.processing.org/t/final-array-is-being-changed/1447
**Category:** Coding Questions
**Created:** [July 2, 2018, 9:24pm UTC](https://discourse.processing.org/t/final-array-is-being-changed/1447 "2018-07-02T21:24:57Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 2, 2018, 10:27pm UTC](https://discourse.processing.org/t/final-array-is-being-changed/1447/4 "2018-07-02T22:27:50Z")

</div>

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

- [https://Forum.Processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array](https://Forum.Processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array)

```java
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();

```

```java
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();

```

---

_[View the full topic](https://discourse.processing.org/t/final-array-is-being-changed/1447)._
