0

I have a simple int array that is initialize like this:

 int [][] tempArray=bp.getArray();//get array from other class(bp)
 //doing things with tempArray...

When I change the tempArray it also change the bp.getArray();. Is this because is it referenced to the bp.getArray();?

2
  • this totally depends on how bp.getArray(); is implemented. Commented May 2, 2013 at 10:41
  • 2
    Arrays are objects and your method returns a reference to the same array object, as demonstrated by the fact that altering it through one reference is seen by all other references. Commented May 2, 2013 at 10:41

2 Answers 2

4

Yes, if you want a new reference it's easy, you have to use Arrays.copyOf. Other functions are Array.clone and System.arrayCopy.

Arrays.copyOf// is a Java 6 feature. For older versions switch to System.arrayCopy.

    public static int[][] deepCopy(int[][] original) {
        if (original == null) {
              return null;
        }

        final int[][] result = new int[original.length][];
        for (int i = 0; i < original.length; i++) {
            result[i] = Arrays.copyOf(original[i], original[i].length);
        }
        return result;
    }

This will deep copy a 2d array.

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

5 Comments

Question: Will this do a shallow or deep copy operation? Since we're dealing with a two-dimensional array, if Arrays.copyOf just copies the references to each inner array, we're going to have a problem.
@alex23 What if I create just a new instance?
I create a new instance of an Array and the I copy the second array? Or it stil be referenced?
Thanks for help. I was just hoping there was simpler solution.
-1

The correct answer is, that you need a deep copy. You're assigned references (pointers), rather than assigning values.

Arrays.copyOf() or System.arraycopy() will work -- but only on the bottom level of int[].

At the int[][] level, if you run a copy, you'll get a separate top-level array pointing to the same bottom-level arrays; not a proper deep copy.

To do it properly (at 2 levels):

public int[][] copy2Deep (int[][] src) {
    int[][] dest = new int[ src.length][];
    for (int i = 0; i < src.length; i++) {
        dest[i] = Arrays.copyOf( src[i], src[i].length);
    }
    return dest;
}

This should work -- the fundamental principle is that int[][] is really an single-dimensional array of int[] with some syntactic sugar added.

Because of this, the bottom dimension is not needed to be known to create the array (it will be filled with 'null' child arrays initially), and src.length can be used to return the length of the top dimension.

See also: Efficient System.arraycopy on multidimensional arrays

Hope this helps.

Comments

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.