Results 1 to 7 of 7
  1. #1
    innspiron is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default Arrays.sort... why sorting all arrays in class?

    Hi,

    part of the code:
    . . .
    public static Integer arrayA [][];
    public static Integer arrayB [][];
    . . .
    arrayA = .... //some values
    . . .
    arrayB = arrayA;

    Arrays.sort(arrayB, new MatrixRowComparator());
    . . .


    The problem and question is why it sorts and arrayB and even arrayA? I call the method for srting only with arrayB.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The answer: Arrays are objects and array variables are reference variables. So that means that this line here:
    Java Code:
    arrayB = arrayA;
    indicates that the arrayB variable should now refer to the same object that is referred to by the arrayA variable. So any changes to this single object that is referenced by the two variables will seem to be seen by the two variables.

  3. #3
    innspiron is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    thank you Fubarable for the answer and for quick response.

    how do make these arrays equal but keep synchronization away? do I have to use loops for every element in array or there is another way? because arrayA is very long and using loops would take too much computation.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The only solution that I know of is to have the array variables refer to two completely different array objects. Perhaps you want to use System.arraycopy(...) here.

  5. #5
    innspiron is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    done. thanks a lot. i love this forum. the response is awesome.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're quite welcome!

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by innspiron View Post
    thank you Fubarable for the answer and for quick response.

    how do make these arrays equal but keep synchronization away? do I have to use loops for every element in array or there is another way? because arrayA is very long and using loops would take too much computation.
    Note that System.arraycopy() likely uses exactly as much computation as doing it with your own loops, but it does have the advantage of being simpler. In other words, it probably doesn't make your program run any faster, but it does make it easier to understand, and that's reason enough for using it.

    -Gary-

Similar Threads

  1. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM
  2. Arrays.Sort usage
    By parthpatel in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 12:26 PM
  3. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  4. Sorting Arrays by enum
    By sahity1a@yahoo.com in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 09:08 AM
  5. How to sort 2 arrays together
    By masaka in forum New To Java
    Replies: 13
    Last Post: 05-21-2008, 04:36 AM

Posting Permissions

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