-1

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.

5
  • Why don't you test it, find out what happens, and then ask clarifying questions of anything you don't understand? Commented Mar 13, 2012 at 13:36
  • 1
    I don't think int[][] is an object, so you don't need to worry about that. Commented Mar 13, 2012 at 13:37
  • 8
    @drgomesp: Yes, all arrays are reference types. Commented Mar 13, 2012 at 13:37
  • 1
    Possible duplicate: stackoverflow.com/questions/419858/… Commented Mar 13, 2012 at 13:40
  • @Alexander Corwin because i wanted you to ask me that question. Commented Mar 13, 2012 at 13:47

4 Answers 4

2

but will the above code copy each element from array into newArray as value (so... a clone of the item), or just the reference?

You're copying each element of the array, and each element is an int, so you're fine. The new array will be completely independent of the original.

Note that if instead you'd done:

int[][] newArray = new int[3][];
for (int i = 0; i < 3; i++) {
    newArray[i] = array[i];
}

... then that would just have copied references to the three existing int[] arrays into newArray. But you've allocated a completely new set of arrays (one int[][] and 3x int[]) so it's all independent.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use clone() on the matrix and on each array corresponding to a row in the matrix, it will work without problems because you're cloning a matrix of primitive values, like this:

int[][] matrix = new int[3][3];
// ... matrix gets filled ...
int[][] copy = matrix.clone();
for (int i = 0; i < matrix.length; i++)
    copy[i] = matrix[i].clone();

The above will create a copy matrix which is independent of matrix, meaning that you can change the values of the copy without affecting the original.

7 Comments

There's no point in the first clone() call - given that you're going to overwrite each element, why not just use int[][]copy = new int[3][];?
@JonSkeet I initially had my answer like that (int[][] copy = new int[matrix.length][]), but changed it for using a clone() operation. I don't think there's a noticeable difference in performance with either option, it's more a matter of consistency - if it's a clone, it's a clone all over.
I think it's misleading as it is. You don't want a copy of the array references, so why write code as if you do? I think it would be much clearer without the first clone call. If you were trying to explain to someone you wanted the code to do, would you say, "Now we want to create an array of int[] references which are the same as the existing int[] references in the original array"?
I want a clone of the matrix, and for me it's more clear in this way.
I'd say that with your current code, it will be tempting for some maintenance engineer to ask themselves, "Why are we bothering to clone the "sub"-arrays? We're already cloning the array in the previous line! Let's just get rid of that loop..." Bang. You're doing work that isn't required, which makes it look like you want that work to be done... which makes the purpose of the code less clear.
|
1

Primitive types, such as int, are not reference types. Thus, going through all the items and copying them one by one will make a copy-by-value.

In short, your code is correct.

2 Comments

then why would these guys say otherwise? stackoverflow.com/questions/9644896/…
@AndreiBogdan: The array itself is a reference. That answer is correct. But each int item of the array is a value.
1

int is a primitive type, you always pass them around as value, not as reference, so you code will indeed create a new copy of the array.

You might want to consider using Arrays.copyOf(), it may be faster.

3 Comments

then why would these guys say otherwise? stackoverflow.com/questions/9644896/…
int is primtive type , int[][] is't primtive type.
I am not saing that int is not a primitive type, nor am I saying that an array of primitives is primitive. I said his code creates a new copy, which it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.