Java Vector Methods
Summary: The most used DS is Vector. Developer is required thorough with all the Java Vector Methods. Be comfortable to go through the tutorial.
Vector internally is a array-based data structure (linked list is node-based) and grows dynamically when more elements are .
Usage of Vector Constructors – Internal mechanism
Four constructors exist with Vector class.
A vector comes with two properties – size and capacity. When a vector object is created, it comes with a default capacity that can store 10 elements. In the above code, vect1 capacity is 10. When 10 elements capacity is exhausted, it adds automatically another 10 and this process goes on. It is like StringBuffer where default capacity is 16 and grows by 16 when the buffer exhausted.
Vector stores the elements in the form of an array. Initially, the array size is 10 (it becomes the capacity to the vector). When more elements are added, it adds one more 10 capacity. How it adds? It searches for a new memory location sufficient to store 20 elements, copies the old location 10 elements into the new location and the old location is garbage collected. So, whenever the capacity gets exhausted, it is a big overhead to the OS. To have performance, we must give less memory management to the OS to execute the program. We have seen this in file copying using BufferedInputStream where performance increased few thousand times than using
To overcome this to some extent, the second vector object vect2 is created with a default capacity of 50. When 50 used out, it adds one more 50 (not 10 as in vect1). If 100 gets exhausted, it adds one more 100 and like on. This type of coding can be chosen when the programmer knows the capacity he requires when the vector object creation itself. But note, after creating initial capacity, for each addition of more elements than capacity, it is a overhead to the OS.
When the memory is very small, incrementing by double the existing capacity may not be bearable to the programmer. The third vector object vect3 takes 50 default capacity and increments by 2 only. That is, it becomes 52, 54 and 56 etc. This again kills the performance. This must be played only when the programmer stores very a few above 50.
The fourth vector object vect4 is created with the elements of vect3 added implicitly. Anyhow, as usual, vect4 can add extra elements one by one with add() method.
Note: A vector capacity cannot be below the size; it may equal.
Following is the class signature of Vector class
public class Vector extends AbstractList implements List, RandomAccess, Cloneable, Serializable
Observe, vector implements List. So Vector, a legacy class, can use the latest methods of List, one of the interfaces of collections framework, like iterator(), add() etc. Now the vector is a member of collections classes. Infact, a vector can be converted into a list or list can be converted into a vector because the super class of both is Collection interface. It is shown in Vector Play With.
Java Vector Methods
Following are some important methods
- void addElement(Object obj): Adds the element obj to the vector. It appends to the existing. The method does not return any value.
- boolean add(Object obj): Adds the element obj to the vector. Return true if added else false.
- void add(int ind, Object obj): Inserts the element obj at the specified index ind.
- boolean addAll(Collection col): Adds all the elements of Collection col to the existing elements in the vector. Adds at the end of the existing elements implicitly. Returns true if added successfully.
- boolean addAll(int ind, Collection col): Inserts all the elements of Collection col to the existing elements in the vector at the specified index ind. Returns true if added successfully.
- boolean remove(Object obj): Removes the element obj from the vector. Returns true if deleted successfully.
- Object remove(int ind): Deletes the element from the vector specified with index number ind. The element deleted is returned.
- void removeElementAt(int ind): Deletes the element at the specified index number ind. This method returns void. Earlier remove() returns the object deleted..
- boolean removeElement(Object obj): Deletes the element by its name obj (not by index number). If multiple elements exist on the same name, the first occurrence is deleted.
- void removeAllElements(): Deletes all the elements from the vector. Now, if size() is called, it prints 0. This method returns void.
- boolean removeAll(Collection col): Deletes all the elements present in the Collection col (vector). Returns true if the method call is successful.
- void removeRange(int start, int end): Removes elements from the vector starting from start and ending with end-1.
- void clear(): Removes all the elements. It is inherited from Collection interface. It is equivalent to removeAllElements().
- int size(): Returns an int value, the number of elements existing in the vector.
- int capacity(): Returns the storage capacity of the vector.
- void copyInto(Object ar1): Copies the elements of vector into the array ar1. See that the size of the array should accommodate all the elements of the vector. Else, it throws ArrayIndexOutOfBoundsException.
- void trimToSize(): Generally, this method is called by the programmer at the end of addition of all elements. This method removes extra capacity and keeps the capacity just to hold the elements. After this method call, the capacity and size will be the same.
- void ensureCapacity(int cap): It increases the capacity by cap.
- void setSize(int size1): It sets the size to size1. If the size1 is greater than the actual number of elements, the extra capacity is filled with null values. If the size1 is less than the number of elements, the extra elements beyond the size1 are deleted. This method is used by the programmer when he would like remove some elements.
- boolean isEmpty(): Checks whether elements exist or not. If no elements exist, it returns true and if exist, returns false.
- Enumeration elements(): Returns an Enumeration object used to iterate and print the elements.
- boolean contains(Object obj): Used to check the existence of an element, say obj. If the element obj exist, it returns true else false.
- int indexOf(Object obj): It works exactly same that of String. It returns the index number of the element obj. If a number of elements exist by the same name, it returns the first occurrence. If no element exist by name obj, it returns -1. Remember, the index numbers starts from zero.
- int indexOf(Object obj, int ind): Returns the index number of element obj. The search starts from index number ind but not from beginning. If no element exist by name obj, returns -1.
- int lastIndexOf(Object obj): Returns the index number of the last existence of the element obj. Returns -1 if not element exist.
- int lastIndexOf(Object obj, int ind): Returns index number of the last existence of the element obj. Search starts from the index number ind (and not from last element). Returns -1 if no element exist.
- Object elementAt(int ind): Returns the element existing at index number ind. Returns the element at the index number ind. If the ind is more than the size of the vector or negative number, it throws ArrayIndexOutOfBoundsException.
- Object firstElement(): Returns the first element (index zero). It is equivalent to elementAt(0). If no elements exist, it throws NoSuchElementException.
- Object lastElement(): Returns the last element. If no elements exist, it throws NoSuchElementException.
- Object set(int ind, Object obj): Replaces the original element present at index ind with the new element obj.
- void setElementAt(Object obj, int ind): Replaces the original element present at index ind with the new element obj. The difference with set() is sequence of parameters and return value.
- Object clone(): The vector object is cloned. The cloned object will have the same elements of original and at the same time maintains different locations thereby perfect encapsulation ismaintained.
- Object toArray(): Returns an array containing the same elements of vector.
- Object get(int ind): Returns the element present at the index number ind.
- boolean containsAll(Collection col): Returns true if the vector and collection col elements, passed as parameter, are same.
- void insertElementAt(Object obj, int ind): Inserts the element obj at the specified index ind. The original element is pushed by one.
- boolean retainAll(Collection col): Retains all the elements in the vector that also exist in Collection col. The other elements in vector are deleted.
- List subList(int start, int end): Returns the elements, as a List object, starting from start and ending with end-1 existing in the vector.