Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing in Array in Ascending order

 
Timothy Scott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but it also has to print the original array. so thats why I can't use that
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:

Please don't do that. Use either clone() or System.arraycopy:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob .
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic