Java Vector Methods


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.

class Vector

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.

  1. Vector vect1 = new Vector();
  2. Vector vect2 = new Vector(50);
  3. Vector vect3 = new Vector(50, 2);
  4. Vector vect4 = new Vector(vect3);

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
FileInputStream alone.

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

  1. void addElement(Object obj): Adds the element obj to the vector. It appends to the existing. The method does not return any value.
  2. boolean add(Object obj): Adds the element obj to the vector. Return true if added else false.
  3. void add(int ind, Object obj): Inserts the element obj at the specified index ind.
  4. 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.
  5. 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.
  6. boolean remove(Object obj): Removes the element obj from the vector. Returns true if deleted successfully.
  7. Object remove(int ind): Deletes the element from the vector specified with index number ind. The element deleted is returned.
  8. void removeElementAt(int ind): Deletes the element at the specified index number ind. This method returns void. Earlier remove() returns the object deleted..
  9. 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.
  10. void removeAllElements(): Deletes all the elements from the vector. Now, if size() is called, it prints 0. This method returns void.
  11. boolean removeAll(Collection col): Deletes all the elements present in the Collection col (vector). Returns true if the method call is successful.
  12. void removeRange(int start, int end): Removes elements from the vector starting from start and ending with end-1.
  13. void clear(): Removes all the elements. It is inherited from Collection interface. It is equivalent to removeAllElements().
  14. int size(): Returns an int value, the number of elements existing in the vector.
  15. int capacity(): Returns the storage capacity of the vector.
  16. 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.
  17. 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.
  18. void ensureCapacity(int cap): It increases the capacity by cap.
  19. 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.
  20. boolean isEmpty(): Checks whether elements exist or not. If no elements exist, it returns true and if exist, returns false.
  21. Enumeration elements(): Returns an Enumeration object used to iterate and print the elements.
  22. boolean contains(Object obj): Used to check the existence of an element, say obj. If the element obj exist, it returns true else false.
  23. 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.
  24. 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.
  25. int lastIndexOf(Object obj): Returns the index number of the last existence of the element obj. Returns -1 if not element exist.
  26. 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.
  27. 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.
  28. Object firstElement(): Returns the first element (index zero). It is equivalent to elementAt(0). If no elements exist, it throws NoSuchElementException.
  29. Object lastElement(): Returns the last element. If no elements exist, it throws NoSuchElementException.
  30. Object set(int ind, Object obj): Replaces the original element present at index ind with the new element obj.
  31. 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.
  32. 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.
  33. Object[] toArray(): Returns an array containing the same elements of vector.
  34. Object get(int ind): Returns the element present at the index number ind.
  35. boolean containsAll(Collection col): Returns true if the vector and collection col elements, passed as parameter, are same.
  36. void insertElementAt(Object obj, int ind): Inserts the element obj at the specified index ind. The original element is pushed by one.
  37. boolean retainAll(Collection col): Retains all the elements in the vector that also exist in Collection col. The other elements in vector are deleted.
  38. 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.

4 thoughts on “Java Vector Methods”

  1. Dear sir,
    A vector capacity cannot be below the size; it may equal.
    What does it mean?
    Capacity increases automatically as explained above –> there is no question of size

  2. Hello sir, I think there is a mistake: the second vector object vect2 is created with a default capacity of 50. When 50 used out, it adds 50 more not 10!!!
    and when 100 used out, it adds 100 more and then capacity becomes 200. i tried this.

Leave a Comment

Your email address will not be published.