We have already discussed ArrayLists and LinkedList. We also discussed difference between ArrayList and Lists in Java.
I suggest you review them:
However, today, we would learn about Vectors. You use the Vector class to implement a dynamic array in Java. Similar to ArrayLists, Vectors also derives from the List class.
But unlike ArrayLists:
- Vector contains additional methods not in collection framework
- Vectors are synchronised (they are thread-safe)
How to Create a vector
Similar to how you create an ArrayList, you can also create a vector using its constructor. Example is given below.
Vector vector1 = new Vector(); Vector<String> vector2 = new Vector<String>();
The first line creates a vector of objects while the second line creates a vector of Strings.
You can as well use any of the constructor listed below
| SN | Constructor and brief description |
|---|---|
| 1. | Vector( )
Creates a vector of initial size of 10. The default data type is object. We already used this |
| 2 | Vector(int size)
Accepts a parameter of the initial size of the vector . Then creates a vector whose initial capacity is the given size |
| 3 | Vector(int size, int incr)
Creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies number of elements to allocate any time the vector is resized upward. |
| 4 | Vector(Collection c)
This constructor creates a vector from the elements of collection c. So the elements of the collection would be transferred to the new vector. |
The program below creates a vector of Strings, vector2.
Then uses the add() method to add 3 strings to the vector
Finally uses an enhanced for loop to print out the elements of the vector
import java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector<String> vector2 = new Vector<String>(); vector2.add("Kindson"); vector2.add("Saffon"); vector2.add("Oleander"); for (String element :vector2) { System.out.println(element); } } }
I would then provide a list of methods available in a vector class. You can therefore use these methods to perform any kind of operations on a vector.
| SN. | Method and brief description |
|---|---|
| 1 | add(int index, Object element)
Inserts the specified element at the position(given by index) in this Vector. |
| 2 | add(Object o)
Adds an element to the end of the Vector. |
| 3 | boolean addAll(Collection c)
Copies all of the elements in the given Collection to the end of this Vector, in the order that they are returned by the given Collection’s Iterator. |
| 4 | addAll(int index, Collection c)
Copies all of the elements in in the given Collection c, into the Vector at the specified index. |
| 5 | addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size of the vector by one. |
| 6 | int capacity()
You use this to get the capacity of the Vector |
| 7 | clear()
Deletes all of the elements of the vector. |
| 8 | clone()
Returns a clone of the vector as an object |
| 9 | contains(Object elem)
Checks if the given object is an element in the vector. |
| 10 | containsAll(Collection c)
Checks if the vector contains all of the elements in the given Collection. Returns true if yes. Otherwise, returns false. |
| 11 | copyInto(Object[] anArray)
Copies the elements of this vector into the given array. |
| 12 | elementAt(int index)
Returns the element at the given index. |
| 13 | Enumeration elements()
Used to returns an enumeration of the elements of the vector. |
| 14 | ensureCapacity(int minCapacity)
Used to increase the capacity of the vector to the capacity specified by minCapacity |
| 15 | equals(Object o)
Uses to check is the vector and the spefied object are equal |
| 16 | firstElement()
Used to return the first component (the item at index 0) of this vector. |
| 17 | get(int index)
Used to return the element at the specified position in this vector. |
| 18 | hashCode()
Used to return the hash code value for this vector. |
| 19 | indexOf(Object elem)
Searches for the first occurrence of the specified parameter, checking for equality using the equals method. |
| 20 | indexOf(Object elem, int index)
Searches for the first occurrence of the specified parameter, beginning at index, and checking for equality using the equals method. |
| 21 | insertElementAt(Object obj, int index)
Inserts the given object as a component in this vector at the given index. |
| 22 | isEmpty()
Checks if this vector is empty. Returns true if the vector is empty. Returns false if othewise |
| 23 | lastElement()
Used to return to return the last element of the vector. |
| 24 | lastIndexOf(Object elem)
Used to return the index of the last occurrence of the specified object in the vector. |
| 25 | lastIndexOf(Object elem, int index)
Searches backwards for the given object, beginning at the specified index, and returns an index the element. |
| 26 | remove(int index)
Removes the particular element at the specified index in the vector. |
| 27 | remove(Object o)
Removes the first occurrence of the given element from the vector, If the vector does not contain the element, it is unchanged. |
| 28 | removeAll(Collection c)
Removes from this vector all of its elements that are contained in the given Collection |
| 29 | removeAllElements()
Takes out all of the elements of this vector and sets its size to zero. |
| 30 | removeElement(Object obj)
Removes the first occurrence of the specified argument from this vector. |
| 31 | removeElementAt(int index)
Removes element at the given index from the Vector |
| 32 | removeRange(int fromIndex, int toIndex)
Removes all of the elements in the vector from fromIndex, inclusive and toIndex, exclusive. |
| 33 | retainAll(Collection c)
Retains only the elements in this vector that are contained in the given Collection. |
| 34 | set(int index, Object element)
Use this to replace the element at the specified position in the vector with the specified element. |
| 35 | setElementAt(Object obj, int index)
Sets the element at the specified index of the vector to be the given object. |
| 36 | setSize(int newSize)
Sets the size of the vector to the newSize |
| 37 | int size()
Used to return the number of components in this vector. |
| 38 | subList(int fromIndex, int toIndex)
Used to return a subList of the vector between fromIndex, inclusive, and toIndex, exclusive. |
| 39 | Object[] toArray()
Used to return an object array containing all of the items in the vector in the correct order. |
| 40 | Object[] toArray(Object[] a)
Used to return an object array containing all of the item in the vector in the correct order; the runtime type of the returned array is that of the given array. |
| 41 | String toString()
Used to return a string representation of the vector, containing the String representation of each of the element. |
| 42 | void trimToSize()
Reduces the capacity of this vector down to the vector’s current size. |
Table 1.1: Methods in the Vector Class
Below is a program that illustrates how to use some of the methods listed above. I recommend you run this code yourself. Also try to use other methods to see how they work.
import java.util.ArrayList; import java.util.Vector; public class VectorDemo2 { public static void main(String[] args) { Vector<String> vector2 = new Vector<String>(); vector2.add("Kindson"); vector2.add("Saffon"); for (String element :vector2) { System.out.println(element); } System.out.println("Current Size: " + vector2.size()); System.out.println("Current Capacity: " + vector2.capacity()); vector2.ensureCapacity(25); System.out.println("New Size: " + vector2.size()); System.out.println("New Capacity: " + vector2.capacity()); vector2.addElement("Oleander"); System.out.println(vector2.isEmpty()); } }
Program to demonstrate use or Vector Methods