I have defined an int[][]
object. Because it is an object, if i send it to a method as a parameter, it will only send it's reference, so any changes to the array in the method, will influence it in the main program. So i would like to make a clone of this object inside the method, but i'm not sure how to accomplish this.
I was thinking of something like so:
private void myMethod( int[][] array )
{
//Define our temporary array (clone)
int[][] newArray = new int[3][3];
//Go through the elements of the array
for .... row = 0; row < ..; row++
for ..... col = 0; col < ..; col++
//Copy individual elements from one array to another
newArray[row][col] = array[row][col];
}
but will the above code copy each element from array into newArray as value (so... a clone of the item), or just the reference?
If so, how can this be accomplished. If i were to use ArrayLists instead of int[][]
objects, there is the clone() method or something like that, but i haven't got that method for int[][]
objects :(
Also, if i'm not mistaken if i do this inside the method newArray = array
, that will copy just the reference again, so both will point to the same int[][]
object :(
P.S. I know i could just test this, but i'd like to discuss it with you guys a bit, and see what's what exactly.