How do I copy a 2 Dimensional array in Java?

I need to make a copy of a fairly large 2 dimensional array for a project I am working on. I have two 2D arrays:


I also have two methods to do the copying. I need to copy the array because current is regularly being updated.

public void old(){


public void keepold(){

However, this does not work. If I were to call old, make an update on current, and then call keepold, current is not equal to what it was originally. Why would this be?



current=old or old=current makes the two array refer to the same thing, so if you subsequently modify current, old will be modified too. To copy the content of an array to another array, use the for loop

for(int i=0; i<old.length; i++)
  for(int j=0; j<old[i].length; j++)

PS: For a one-dimensional array, you can avoid creating your own for loop by using Arrays.copyOf