0

I've made a 2D array which is a map for a game. As the player moves around the map the Array changes slightly, but I'd like to be able to refer back to the original, unchanged Array. How can I do this?

Thanks a lot.

hunterge
  • 657
  • 2
  • 10
  • 15

2 Answers2

0

If arr is a 2d String array:

String[][] copy = arr.clone();

Then, just make changes to copy.

If it is an array of objects, you may want to make a deep copy, i.e. making a copy of all the contained objects. But in your case, since Strings are immutable, a clone will suffice. However, considering storing your data in classes instead of strings.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
0

If you don't need a copy of each element, just the array, use

array.clone()

If you do need to copy each element, you can see this answer.

Community
  • 1
  • 1
Zoltán
  • 21,321
  • 14
  • 93
  • 134