Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By sunde887

Thread: array1 = array2 if array1 be changed then array2 change automatically?

  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default array1 = array2 if array1 be changed then array2 change automatically?

    hi ,all
    can i change array1 but array2 doesn't be changed by itself?

  2. #2
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default

    and can someone explain how array1=array2 works?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
     
    int[] array1 = new int[10];
    int[] array2 = new int[5];
    array1=array2;
    What it does, and why changes to array1 affect array2 and vice versa is this:

    Both of the above declarations initialize an array object(the new keyword) on the heap. This object is pointed to by the variable name. When you re-assign, you are basically telling the variable name to point to a different object on the heap. So if you fill array 1 with data, and then set it to array2, you will have lost all the data in array1, since it no longer has a reference pointing to it.

    As explained in the above, if both reference(variables) point to the same object on the heap, changes to one will appear to affect the other(it isn't really affecting the other since there is only one object being referenced).
    JeffGrigg and Mustafa Shady like this.

  4. #4
    mr_j936 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: array1 = array2 if array1 be changed then array2 change automatically?

    Hello

    I got into a problem because of this array pointer thingy, I need to make a copy of an array, then work with the copy, without affecting the original array. how can I accomplish that? how do I make a copy of an array that has different pointers than the original array? I hope someone can answer me soon and thanks

  5. #5
    Mustafa Shady is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: array1 = array2 if array1 be changed then array2 change automatically?

    array1=array2;
    but as I know : you are making the object array1 = to the object array2;
    I think it has no Link with the arrays without []

    by my modest experience :)

  6. #6
    mr_j936 is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: array1 = array2 if array1 be changed then array2 change automatically?

    I solved my problem, apparently using the arraycopy method copies the array and makes new references

Similar Threads

  1. SCWCD dumps changed?
    By ajincoep in forum Java Certification
    Replies: 1
    Last Post: 07-12-2011, 08:43 AM
  2. Checking when database was last changed
    By Smoke3723 in forum JDBC
    Replies: 1
    Last Post: 05-07-2010, 07:47 AM
  3. Event for jTtextField changed
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 03-03-2009, 07:55 PM
  4. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 05:56 AM
  5. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •