Thread: sort arrays
View Single Post
Old 04-03-2003, 11:14 AM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
Originally posted by sno2dude
a vector is like an array except that its size changes every time that you add something to it. I dont use it because you have to cast things to put it into a vector and convert them to take it out and do a bunch of other things with it so it will work right.... that and i fell asleep halfway through the class on vectors.
what you implemented is known as bubblesort. it is one of the easiest sorting algorithms to understand (and code). it is also one of the slowest.

you don't have to convert any objects to put them into a vector, since vectors take Object types, and all classes inherit from Object. casting is very simple and safe in java, and is commonly performed in most API data structures. if you have problems with casting, java may not be your thing.

having said that, it seems that i was wrong about vector (i thought it has a sort call, but it doesn't). instead, try the TreeSet class. the code below is untested, but probably comes close to working.

Code:
TreeSet ts = new TreeSet();

ts.add("hi");
ts.add("dude");
ts.add("asdf");

Iterator it = ts.iterator();
while(it.hasNext())
{
  String st = (String)it.next();
  System.out.println(st);
}
joe_bruin is offline   Reply With Quote