| Author |
Printing in Array in Ascending order
|
Timothy Scott
Greenhorn
Joined: Apr 23, 2009
Posts: 7
|
|
I'm having a hard time figuring out how to print this array in ascending order anytime I try to it says it can't find the variable what can I do to fix that and not have an infinite loop
|
 |
jittu goud
Ranch Hand
Joined: Mar 30, 2007
Posts: 46
|
|
i can compile your code with out any edit...but its printing in random order....
if you want to sort out your values you can use the java.util.SortedSet
i have edited and it prints the sorted collection above the 500
here it is
|
 |
Timothy Scott
Greenhorn
Joined: Apr 23, 2009
Posts: 7
|
|
|
yes but it also has to print the original array. so thats why I can't use that
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Arrays.sort method sorts an array in ascending order. You can copy a reference of the array then sort one of the reference and print them simultaneously.
Example:
Hunter.
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Hunter McMillen wrote:
Please don't do that. Use either clone() or System.arraycopy:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Thanks Rob .
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Rob Prime wrote: . . . Use either clone() or System.arraycopy: . . .
As usual, Rob is right.
Remember the clone() method of an array returns a shallow clone, so it works best for an array with one [] pair and immutable objects. If you have an array with more than one pair of [] or which contains mutable objects, the individual elements are copied, not cloned, so a change in the 1st array will be reflected in the clone and vice versa.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Right so if the change is reflected, if you used Arrays.sort on arr1 arr2 would be sorted also correct? doesnt he want to print the original and the sorted?
Hunter.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
The array elements themselves will be the same, but the arrays are definitely decoupled - on a first level.
Consider the following:
So after clone() or System.arraycopy, both arrays have the exact same contents - that includes references to the exact same objects. If you overwrite a reference (like on line 9) that only changes one array. If you change an element of one array (like on line 11), you change the object to which both arrays are referencing, and therefore the contents of both arrays change. Not the references inside the array, but the objects these references refer to.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
No, that is not what I meant at all.
The clone of an array which contains mutable elements, ie mutable objects (which includes arrays, as in a [][] array), contains references to the original objects. If you sort the clone, the original array remains unchanged, but if you change the state of any elements, that element is in both arrays, and you can see the change from both references. Here's some code I thought up out of my head, and executed
[Campbell@localhost java]$ java PhoneBook 123 456 789 234 567 890 321 654 987 147 258 369 741 852 963 753 123 456 123 567
Array before sorting
*************************************************
Phone number: (123) 456
Phone number: (789) 234
Phone number: (567) 890
Phone number: (321) 654
Phone number: (987) 147
Phone number: (258) 369
Phone number: (741) 852
Phone number: (963) 753
Phone number: (123) 456
Phone number: (123) 567
*************************************************
Array copy before sorting
*************************************************
And if you want to know what happens next, you will have to run it yourself
|
 |
 |
|
|
subject: Printing in Array in Ascending order
|
|
|