Reputation: 1
i basically want to reset an array once i have shot all the elements in my array/game. is there a way i can clear my 2D array and basically reset it so that all the elements i have shot and declared null then return?
Upvotes: 0
Views: 408
Reputation: 1474
you can clear array by Loop over the array and and set each element to null.
for( int i = 0; i < yourArray.length; i++ )
Arrays.fill( yourArray[i], null );
you cannot restore past contents except you can make backup (Copy) of your array for later use.
Deep Copy Code:
public int[][] copy(int[][] input) {
int[][] target = new int[input.length][];
for (int i=0; i <input.length; i++) {
target[i] = Arrays.copyOf(input[i], input[i].length);
}
return target;
}
Upvotes: 0
Reputation: 73578
You can keep a (deep) copy of the original array, and at the end just make a new working copy of it.
If you're not making changes to the actual objects inside the array, you could just do a shallow copy with Foo[][] backup = original.clone();
, but if the objects in the array have state (for example health), you'll need to loop the array by hand and create deep copies of the objects.
Upvotes: 2