Results 1 to 7 of 7
- 03-22-2010, 11:35 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 12
- Rep Power
- 0
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
-
The answer: Arrays are objects and array variables are reference variables. So that means that this line here:
Java Code:arrayB = arrayA;
- 03-22-2010, 11:52 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 12
- Rep Power
- 0
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.
-
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.
- 03-23-2010, 12:07 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 12
- Rep Power
- 0
done. thanks a lot. i love this forum. the response is awesome.
-
You're quite welcome!
- 03-23-2010, 02:40 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
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
-
Sorting Two Arrays
By Faye Rett in forum New To JavaReplies: 4Last Post: 03-07-2010, 02:00 AM -
Arrays.Sort usage
By parthpatel in forum New To JavaReplies: 2Last Post: 02-14-2010, 01:26 PM -
Problem: Arrays and Sorting
By Rhez in forum New To JavaReplies: 7Last Post: 02-03-2010, 03:18 PM -
Sorting Arrays by enum
By sahity1a@yahoo.com in forum New To JavaReplies: 3Last Post: 11-26-2009, 10:08 AM -
How to sort 2 arrays together
By masaka in forum New To JavaReplies: 13Last Post: 05-21-2008, 05:36 AM
Bookmarks