wood burning stoves*
The moose likes Beginning Java and the fly likes static final int[] wont print unsorted column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static final int[] wont print unsorted column" Watch "static final int[] wont print unsorted column" New topic
Author

static final int[] wont print unsorted column

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
I'm sorry to post the whole class, but I don't know how to reduce this correctly. The problem is the printSortedScreen method, which is supposed to print the unsorted column first, then the sorted column, but it actually prints both columns sorted. I don't know what I've done wrong..

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The problem is with this method



The first parameter is an object reference. When you sort the array named toSort inside the method, it sorts the array referred to by the parameter you send.

What you need to do is something like create a temporary array inside the method and copy the contents of the array to be sorted into it. Then sort that array instead of the array referred to by toSort.

Making the array reference final does not prevent the elements in the array from being changed. It prevents the array name from pointing to another array.

For example, this works with no problem



, but this causes a compile-time error.


[ February 17, 2006: Message edited by: Keith Lynn ]
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
My heart is broken that final doesn't work the way I thought. But of course it doesn't, no matter how much I say "but I *did* push the jump button"


[ February 17, 2006: Message edited by: Jesse Crockett ]
 
Don't get me started about those stupid light bulbs.
 
subject: static final int[] wont print unsorted column
 
Similar Threads
Trouble comparing fractions
need help sorting Integer objects
sorting fractions problem
does this sort an array of integers?
; expected