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.
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.
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 String
s are immutable, a clone
will suffice. However, considering storing your data in classes instead of strings.
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.