Vector and ArrayList are the more popular ones. I've never used ArrayList, but Vectors work like :
Code:
Vector v = new Vector;
Object o = new Object();
v.addElement(o); //adds an Object to the Vector, and increases its size by 1
v.removeElement(o); //removes the Object from the Vector and decreases its size by 1
v.elementAt(index); //returns the Object at the given index
v.elements(); //returns an Enumeration of all the Objects that the Vector contains
v.contains(o); //returns true if the Object exists in the Vector
Thats the basics of a Vector.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Vector, ArrayList, LinkedList are off the the List interface so depending on your requirments(lists allow duplicate objects, and maintain order) you can opt to use one of these concrete implementations. If you do not wish duplicates to be stored then i would go with using a set. TreeSet is a concrete implementation
which implements the SortedSet interface so this would essentially give you order plus not allow duplicates to be stored.
Last edited by Dilenger4; Mar 17th, 2002 at 07:04 PM.
To loop through a Vector that contains Strings you could do somthing like the following.
Code:
for(Iterator i = v.iterator();i.hasNext;){
String s = (String) i.next();
System.out.println(s);
}
The iterator interface contains the following.
public abstract boolean hasNext();
public abstract Object next();
public abstract void remove();
So what you are essentially doing is invoking a method in the Vector class that returns a refrence or a handle the iterator interface so those methods can be invoked.
One more thing. The vector and hashtable classes are declared synchronized so if you are looking for better performance an ArrayList would be the way to go since it is not declared synchronized.
If you want you can actually synchronize and unsynchronized class through a decorator object.
Code:
Collections syncSet = Collections.synchronizeSet(someset);
// each thread must iterate when synchronized on the decorator
synchronize(syncSet){
for(Iterator i = syncSet.iterator(); i.hasNext();){
processData(i.next());
}
}
Last edited by Dilenger4; Mar 19th, 2002 at 02:54 PM.