Vector class in java.util package is very easy to use. You can add objects of any type in a vector.
It works with any class that implements Comparable interface or has a Compartor method. If you want elements of your own class sorted in a vector you will need to either make your class implement Comparable or create a Comparator method.
public class SortedVector extends Vector{
public SortedVector(){
super();
}
public void addElement(Object o){
super.addElement(o);
Collections.sort(this);
}
}
Here is a sample program showing how to use this class.
public static void main(String args[]){
SortedVector v =new SortedVector();
v.addElement(new Double(12));
v.addElement(new Double(320));
v.addElement(new Double(21));
System.out.println(v);
}
The output from this program is [12.0, 21.0, 320.0].
Popularity: 4% [?]
If you enjoyed this post, make sure you subscribe to my RSS feed!Related posts:
- Import static elements for more readable Java code
- Sort numbers in Javascript array
- Automatically twitter your posts
- Sort numbers in Java to find minimum and maximum values without using Array
- Three ways to find minimum and maximum values in a Java array.






I liked this tip very much