I am making a basic game using a 2D array (4×4) in which the elements (of object type with ints 1 to 16) must be switched around to reach a particular goal state, this state must be compared with the current state, hence the need for copying.
So far I have:
public void cloneArray() throws CloneNotSupportedException
{
ClassName copy = (ClassName)super.clone();
copy.tiles = (Tile[][]) tiles.clone();
}
Does this appear to be right, or am I missing something out?
You’ll need to go one step further and do like so :
The reason is that clone makes a shallow copy of the top-level array, which is holding references to other arrays.