Java Notes: Vectors
Vectors (the java.util.Vector
class)
are commonly used instead of arrays, because they expand
automatically when new data is added to them.
The Java 2 Collections API introduced the similar ArrayList data structure.
ArrayLists are unsynchronized and therefore
faster than Vectors, but less secure in a multithreaded environment.
The Vector class was changed
in Java 2 to add the additional methods supported by ArrayList.
See below for a
reasons to use each. The description below is for the (new) Vector class.
Vectors can hold only Objects and not primitive types (eg, int
).
If you want to put a primitive type in a Vector,
put it inside an object (eg, to save an integer value use the Integer
class
or define your own class). If you use the Integer wrapper,
you will not be able to change the integer value, so it is sometimes useful
to define your own class.
To Create a Vector
You must import either import java.util.Vector;
or import java.util.*;
.
Vectors are implemented with an array, and when that array is full and an additional element
is added, a new array must be allocated. Because
it takes time to create a bigger array and copy the
elements from the old array to the new array, it is a little faster
to create a Vector with a size that it will commonly be when full. Of course, if you knew the
final size, you could simply use an array. However, for non-critical sections of code
programmers typically don't specify an initial size.
- Create a Vector with default initial size
Vector v = new Vector();
- Create a Vector with an initial size
Vector v = new Vector(300);
To Add elements to the end of a Vector
v.add(s); // adds s to the end of the Vector v
To get the elements from a Vector (ListIterator)
You can use a for
loop to get all the elements from a Vector,
but another very common way to go over all elements in a Vector is to
use a ListIterator. The advantage of an iterator is that it it can be used
with other data structures, so that if you later change to using a linked
list for example, you won't have to change your code. Here is an example
of using an iterator to print all elements (Strings) in a vector.
The two most useful methods are hasNext()
,
which returns true if there are more elements, and
next()
, which returns the next element.
ListIterator iter = v.listIterator(); while (iter.hasNext()) { System.out.println((String)iter.next()); }
Common Vector Methods
There are many useful methods in the Vector class and its parent classes.
Here are some of the most useful.
v
is a Vector,
i
is an int index,
o
is an Object.
Method | Description |
v.add(o) |
adds Object o to Vector v |
v.add(i, o) |
Inserts Object o at index i, shifting elements up as necessary. |
v.clear() |
removes all elements from Vector v |
v.contains(o) |
Returns true if Vector v contains Object o |
v.firstElement(i) |
Returns the first element. |
v.get(i) |
Returns the object at int index i. |
v.lastElement(i) |
Returns the last element. |
v.listIterator() |
Returns a ListIterator that can be used to go over the Vector. This is a useful alternative to the for loop. |
v.remove(i) |
Removes the element at position i, and shifts all following elements down. |
v.set(i,o) |
Sets the element at index i to o. |
v.size() |
Returns the number of elements in Vector v. |
v.toArray(Object[]) |
The array parameter can be any Object subclass (eg, String). This returns the vector values in that array (or a larger array if necessary). This is useful when you need the generality of a Vector for input, but need the speed of arrays when processing the data. |
Old and New Vector Methods
When the new Collections API was introduced in Java 2 to provide uniform data structure classes, the Vector class was updated to implement the List interface. Use the List methods because they are common to other data structure. If you later decide to use something other than a Vector (eg, ArrayList, or LinkedList, your other code will not need to change.
Even up thru the first several versions of Java 2 (SDK 1.4), the
language had not entirely changed to use the new Collections methods. For example, the
DefaultListModel
still uses the old methods, so if you are using
a JList
, you will need to use the old method names.
There are hints that they plan to change this, but still and interesting omission.
Replacements for old methods
The following methods have been changed from the old to the new Vector API.
Old Method | New Method |
---|---|
void addElement(Object) | boolean add(Object) |
void copyInto(Object[]) | Object[] toArray() |
Object elementAt(int) | Object get(int) |
Enumeration elements() | Iterator iterator() ListIterator listIterator() |
void insertElementAt(Object, int) | void add(index, Object) |
void removeAllElements() | void clear() |
boolean removeElement(Object) | boolean remove(Object) |
void removeElementAt(int) | void remove(int) |
void setElementAt(int) | Object set(int, Object) |
Insuring use of the new API
When you create a Vector, you can assign it to a List (a Collections interface). This will guarantee that only the List methods are called.
Vector v1 = new Vector(); // allows old or new methods. List v2 = new Vector(); // allows only the new (List) methods.