overwriting arrays

Discussion in 'Java' started by Jeremy Watts, Jun 14, 2005.

  1. Jeremy Watts

    Jeremy Watts Guest

    Hi,

    When I attempt to overwrite or copy one array to another, using something
    like :

    array1 = array2;

    then the two arrays seem to become 'linked' or associated with eachother, so
    that if I made a change to array2 then that same change is also performed on
    array1. Is this unique to Java??

    I have programmed in PHP in the past and it has not seemed to have done
    this. Is there a way to turn this feature off or to get around it somehow.?

    What I am after is a way of simply copying one array to another and to have
    those two separate arrays to behave independently of eachother.

    Thanks
    Jeremy
     
    Jeremy Watts, Jun 14, 2005
    #1
    1. Advertisements

  2. array1 and array2 are array *references* (think: "pointers"). The
    assignment does not change the contents of either array. After the
    operation, array1 and array2 both refer to the *same* array, the one
    previously referred to by array2.
    Not at all.

    If you had done the equivalent operation in C using
    pointers, you would have experienced the same thing.
    System.arrayCopy();

    /gordon
     
    Gordon Beaton, Jun 14, 2005
    #2
    1. Advertisements

  3. In Java every array is an object. This means that array1 and array2 are
    references to objects (like pointers). By setting array1=array2 you are
    pointing array1 to the same object as array2.

    If you want to make a copy of the array take a look at how array cloning.

    Pan
    ============================================================================
    TechBookReport Java http://www.techbookreport.com/JavaIndex.html
     
    TechBookReport, Jun 14, 2005
    #3
  4. Jeremy Watts

    Jeremy Watts Guest

    i've tried that, but it still seems to 'associate' the two arrays. For
    example I used:
    System.arraycopy(Pmatrix, 1, Lmatrix, 1, size);

    which copies Pmatrix to Lmatrix, but still seems to have the effect of
    linking changes between the two. anything im missing here?
     
    Jeremy Watts, Jun 14, 2005
    #4
  5. I'll assume that you've in fact created two separate arrays to begin
    with, and that it was your intention to copy from the *second* element
    (at position 1).

    What I wrote earlier about array references is true of references to
    *any* object. The above call to System.arraycopy() does basically
    this:

    for (int i=1; i<size; i++) {
    Lmatrix = Pmatrix;
    }

    If your arrays contain object references, then each array holds its
    own copy of the references afterwards and can be manipulated
    independently of the other, but the referred objects themselves will
    be shared.

    In that case, you need to make copies of the objects (not copies of
    the object references) when you copy the array elements. Exactly how
    you do that depends on what the objects are (and needs their support),
    but look into using clone() or a copy constructor.

    It might look something like this:

    for (int i=1; i<size; i++) {
    Lmatrix = Pmatrix.clone();
    }

    or this:

    for (int i=1; i<size; i++) {
    Lmatrix = new Gurka(Pmatrix);
    }

    /gordon
     
    Gordon Beaton, Jun 14, 2005
    #5
  6. If you create a new array Lmatrix (i.e. int[] Lmatrix = new int[..]) the
    two arrays "aren't linked".

    Have a look at the following:

    int[] array1 = {5, 4, 3, 2, 1, 0};
    int[] array2 = new int[array1.length];
    System.arraycopy(array1, 0, array2, 0, array1.length);
    for (int i=0; i<array1.length; i++) array1 = i;
    for (int i=0; i<array1.length; i++) System.out.print(array1+" ");
    System.out.println();
    for (int i=0; i<array2.length; i++) System.out.print(array2+" ");
    System.out.println();

    The output is:
    0 1 2 3 4 5
    5 4 3 2 1 0

    Now have a look at this (which only differs in the second line):

    int[] array1 = {5, 4, 3, 2, 1, 0};
    int[] array2 = array1;
    System.arraycopy(array1, 0, array2, 0, array1.length);
    for (int i=0; i<array1.length; i++) array1 = i;
    for (int i=0; i<array1.length; i++) System.out.print(array1+" ");
    System.out.println();
    for (int i=0; i<array2.length; i++) System.out.print(array2+" ");
    System.out.println();

    The output is:
    0 1 2 3 4 5
    0 1 2 3 4 5

    The reason is that in the first example you have two arrays, the first
    referenced by array1 and the second referenced by array2. In the second
    example you have just one array because array2 references the same array
    as array1. (There is no second array created but only the reference to
    the first array is copied.)

    One more comment on the line
    System.arraycopy(Pmatrix, 1, Lmatrix, 1, size)
    Notice that the first element of an array has the index 0 not 1.

    Greetings

    Alex
     
    Alexander Lueck, Jun 14, 2005
    #6
  7. If the element type of the arrays is (also) a reference type, then copying the
    arrays and then fiddling with the contents, would have about the same effect
    as your first attempt. When copying objects that contain objects (that
    contain objects), a so-called "deep copy" will recursively copy every bit of
    information. The java.util.Arrays class provides methods for doing this.
     
    Boudewijn Dijkstra, Jun 14, 2005
    #7
  8. Alexander Lueck coughed up:

    This isn't going to help him if what he's got is an array of objects (object
    references). I'm fairly sure that that is what's confusing him. He's
    probably building two distinct arrays, but each holding the same references
    as the other.

    This would make it /look/ as if the arrays were linked.


    ....[rip]...
     
    Thomas G. Marshall, Jun 15, 2005
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.