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

need help sorting Integer objects

 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to run a selection sort method on a Vector array of Integer objects. Does Integer autoboxing apply in the following code, as I have written it?

 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume it does, if the compiler doesn't complain. May I attempt to answer your other question by pointing out that (unlike arrays), when you insert something into a Vector, the index of all entries after the inserted entry gets increased by 1? I found it hard to get that right when I tried sorting-like activities on Lists.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, take a look at the API for insertElementAt (better: add(index, object)) versus set(index, object):

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better yet, take a look at the static method java.util.Collections.sort().
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic