We have already mentioned in our previous post about variables in the Java language that the dimensions of arrays are established as part of the array initialization, and cannot be dynamically modified.
To overcome this limitation, the Vector class in the java.util package implements a kind of variable-length array.
Declaration and initialization of a Vector variable
In the sample code below we can see how a Vector variable is declared, and some elements are added to it:
Vector<String> v = new Vector<String>();
v.add("This is the second element in the v Vector");
v.add("Third line, at index 2 in the v Vector");
System.out.println("Number of elements in Vector v : "+v.size());
System.out.println("First element: "+v.elementAt(0));
- A Vector variable is initialized with a call to “new Vector()” that creates an instance of the Vector class.
- In the variable declaration, the type of objects that can be stored in the Vector are also specified (String objects, in the example above).
It is possible to omit the object type specification. However, in this case the compiler generates a warning (although it compiles successfully). If we want to keep the freedom to assign objects of any kind while avoiding this warning, we can just define the vector as Vector<Object>.
- Elements are added to the vector by means of calls to the add() method.
- The size() method returns the number of elements in the vector
- The elementAt(index) method is used to retrieve the element at that index. Vector indexes are numbered starting from zero.
Adding, removing and retrieving elements in a Vector
We have already mentioned the add(elm) method used to add an element at the end of the Vector. Besides, these other methods to handle the elements are available in the Vector class:
addElement(elm) – Same as add(e).
add (idx, elm), insertElementAt(elm, idx) – insert an element ‘elm’ at the specified index
set (idx, elm), setElementAt(elm, idx) – replace with element ‘elm’ the element at index ‘idx’
get (idx), elementAt(idx) – retrieve element at index ‘idx’
firstElement(), lastElement() – Retrieve the first/last element in the vector
remove (idx), removeElementAt(idx) – Retrieve the element at index ‘idx’, and remove it from the vector.
The index of elements after the element being removed is decremented: Element previously at index ‘idx+1’ becomes the new element at index ‘idx’, and so forth
removeRange(idx1, idx2) – remove elements with index ‘idx’ in the range idx1 <= idx < idx2
contains( value ) – Returns true if the value of one of the elements in the vector is the object passed as argument.
Capacity of a Vector
When an element is added to a vector, the system needs to allocate the memory required to store a reference to it.
To improve the performance when working with vectors that must contain a large number of elements, an ‘initial capacity’ and a ‘capacity increment’ can be specified in the variable declaration:
int initialCapacity = 1000;
int capacityIncrement = 100;
Vector v = new Vector(initialCapacity, capacityIncrement );
When the vector variable is created, the system allocates enough memory to hold ‘initialCapacity’ elements in it.
Every time more memory is required, the system will allocate enough memory to hold ‘capacityIncrement’ additional elements.
Besides, the following methods are related to the capacity of a vector variable:
int capacity() – Returns the current capacity of the vector (It must always be greater or equal to the current size of the vector).
ensureCapacity(int capacidad) – Increments the vector capacity if it is lower than the argument supplied (but does not reduce it if it is greater)
trimToSize() – reduces the vector capacity to equal the current size (number of elements) of the vector.