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

static final int[] wont print unsorted column

 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic