Vectors
Vectors
Creating a Vector
import java.util.*; Vector vec1 = new Vector();
void removeAllElements()
Removes all elements
Object firstElement()
Returns the component at location 0
Object lastElement()
Returns the last component
Searching a Vector I
boolean contains(Object elem)
Tests if elem is a component of this Vector
Searching a Vector II
int lastIndexOf(Object elem)
Returns the index of the last occurrence of elem in this Vector Returns -1 if elem was not found in this Vector
int size()
Returns the number of elements in this Vector
Object[ ] toArray()
Returns an array containing all the elements of this Vector in the correct order
A minor nuisance
Suppose you define
Vector vec = new Vector(); Rabbit bunny = new Rabbit();
You can do
vec.add(bunny);
Conclusion
A Vector is like an array of Objects The advantage of a Vector is that you dont need to know beforehand how big to make it The disadvantage of a Vector is that you cant use the special syntax for arrays
You should never use an array that you hope is big enough--use a Vector instead
The End