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
  • 647
  • 2
  • 9
  • 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
  • 33,820
  • 20
  • 135
  • 218
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
  • 20,250
  • 12
  • 87
  • 128