just wondering which sorting method to use, arrays.sort or collections.sort??!!
my vector array contains person objects person(name, surname, age, number)
Joined: Dec 06, 2001
It depends. Are you using an array (such as Person) or a Collection (such as Vector, ArrayList, Set, etc.)? If you are using an array, then you should use Arrays.sort(). If you are using a Collection, then you should use Collections.sort(). (Notice that the names of these classes are Arrays and Collections. Java is case-sensitive; in other words, a lower-case letter is very different from an upper-case one.)
im using vector but i dont know if its the most appropriate one to use!!! what i need to do is sorting Person objects in 4 different orders,add, remove, and find person object! ive read about vector being synchronised and arrayList being not, but i dont think it clicked on my head what it means and whether mine needs to be synchronised or not !!
"synchronized" (in this context) means that only one thread at a time can manipulate the contents of a collection. It depends entirely how you are using the collection as to whether this is is something you want. Typically though, you would not use Vector, since it effectively hardwires manipulation of collection in a synchronized way which many not always be appropriate. If you are unsure - descibe how it is used in your app and perhaps someone can advise.
aha, thanks so really i dont think i need it!! cus the only 2 method that will manipulate the collection is the add and remove!! but in the add(), there will be a call made to find() to c if the person is already there or not! is that counted as manipulating it using more than one thread at that time!?
can some1 give me an example of where it will be appropriate to use Vector? ArrayList?
hannah [ January 19, 2005: Message edited by: H Melua ]
Joined: Dec 06, 2001
This has been discussed a couple of times in the past few weeks on this forum. It seems that the general concensus here at the Ranch is to NEVER use Vector. Apparently it is an antiquated class. Instead you should always use ArrayList. If you need to synchronize the list for a multi-threaded program, then you can call Collections.synchronizedList() to create a synchronized list from an ArrayList.
Now back to your original question. Even after my response, you indicated that you still don't know whether to use Arrays.sort() or Collections.sort(). Hopefully I can clarify. Whenever you use a class that implements the Collection interface, even if indirectly, you should use Collections.sort(). Both ArrayList and Vector implement Collection, so you should use Collections.sort() to sort them.
Perhaps it will help to point you to the javadocs for the Collections class and the Arrays class. If you scroll down to the methods we are discussing here, you can see that there are two versions of Collections.sort(). One takes a List as a parameter and the other takes a List and Comparator as parameters. Compare this to the various versions of Arrays.sort(). This method always takes an array of some sort as one of its parameters.
IMO, it will be very helpful for you to familiarize yourself with the Java API documentation. This link gives an index to all the API docs available. You should find a link to the documentation for the JDK version that you are using. As I illustrated above, sometimes you can determine which method to use based on the parameter types given in the docs.