Vector Class in Collection Framework

java.util.Vector;
public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable
Vector class provides the implementation of List interface through AbstractList class. An object of type Vector represents a dynamic array. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the code Vector has been created.
Vector class features:
  1. Vector is used for Dynamic and Resizable-array implementation of the List interface.
  2. It is used to store an “insertion ordered” group of elements.
  3. It can contain duplicate elements.
  4. Implements all optional list operations, and permits all elements, including null.
  5. It is synchronized.
  6. The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.
  7. An Vector capacity is the size of the array used to store the elements in the list.
  8. As elements are added to an Vector, its capacity grows automatically.
Constructor of Vector
  1. Vector()
    It is used to create  an empty list with an initial capacity of ten.
  2. Vector(Collection c)
              It is used to create a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
  3. Vector(int initialCapacity)
             It is used to create an empty vector with the specified initial capacity and with its capacity increment equal to zero.
  4. Vector(int initialCapacity, int increment)
             It is used to create an empty an empty vector with the specified initial capacity and capacity increment
Method of Vector class
  1. void add(int index, Object element)
             It is used to insert the specified element at the specified position in this list (optional operation).
  2. boolean add(Object o)
             It is used to append the specified element to the end of this list (optional operation).
  3. boolean addAll(Collection c)
              It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
  4. boolean addAll(int index, Collection c)
               It is used to insert all of the elements in the specified collection into this list at the specified position (optional operation).
  5. void addElementl(Object obj)
               It is used to add the specified component to the end of this vector, increasing its size by one.
  6. int capacity ()
               It is used to return the current capacity of this vector.
  7. void clear()
             This method is used to remove all of the elements from this vector.
  8. Object clone()
             This method is used to return clone of this vector.
  9. boolean contains(Object o)
             This method returns true if this list contains the specified element.
  10. boolean containsAll(Collection c)
              This method returns true if this list contains all of the elements of the specified collection.
  11. void copyInto(Object[] anArray)
    This method copies the components of this vector into the specified array.
  12. Object elementAt(int index)
    This method returns the component at the specified index.
  13. Enumeration elements()
    This method returns an enumeration of the components of this vector.
  14. void ensureCapacity(int minCapacity)
    This method increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
  15. boolean equals(Object o)
              This method compares the specified object with this list for equality.

  16. Object firstElement()
    This method returns the first component (the item at index 0) of this vector.
  17. Object get(int index)
              This method returns the element at the specified position in this list.
  18. int hashCode()
              This method returns the hash code value for this list.
  19. int indexOf(Object o)
              This method returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
  20. int indexOf(Object elem, int index)
    This method searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.
  21. void insertElementAt(Object obj, int index)
    This method inserts the specified object as a component in this vector at the specified index.
  22. boolean isEmpty()
              This method returns true if this list contains no elements.
  23. Object lastElement()
    This method returns the last component of the vector.
  24. int lastIndexOf(Object o)
              This method returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
  25. int lastIndexOf(Object o , int index)
              This method searches backwards for the specified object, starting from the specified index, and returns an index to it.
  26. Object remove(int index)
              This method removes the element at the specified position in this list (optional operation).
  27. boolean remove(Object o)
              This method removes the first occurrence in this list of the specified element (optional operation).
  28. boolean removeAll(Collection c)
             This method removes from this list all the elements that are contained in the specified collection (optional operation).
  29. void removeAllElements()
    This method removes all components from this vector and sets its size to zero.
  30. boolean removeElement(Object obj)
    This method removes the first (lowest-indexed) occurrence of the argument from this vector.
  31. void removeElementAt(int index)
    This method remove the element at the specified position in this list.
  32. protected void removeRange(int fromIndex, int toIndex)
    This method removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
  33. boolean retainAll(Collection c)
              This method retains only the elements in this list that are contained in the specified collection (optional operation).
  34. Object set(int index, Object element)
             This method replaces the element at the specified position in this list with the specified element (optional operation).
  35. void setElementAt(Object obj, int index)
    This method sets the component at the specified index of this vector to be the specified object.
  36. void setSize(int newSize)
    This method sets the size of this vector.
  37. int size()
              This method returns the number of elements in this list.
  38. List subList(int fromIndex, int toIndex)
              This method returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
  39. Object[] toArray()
              This method returns an array containing all of the elements in this list in proper sequence.
  40. Object[] toArray(Object[] a)
              This method returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
  41. String toString()
    This method returns a string representation of this Vector, containing the String representation of each element.
  42. void trimToSize()
    This method trims the capacity of this vector to be the vector's current size.
Example of Vector class

import java.util.Vector; import java.util.Enumeration; import java.util.Iterator; import java.util.ListIterator; public class VectorDemo { public static void main(String[] args) { // Vector Creation Vector v = new Vector(); //Adding elements to the Vector v.addElement("Welcome"); v.add("to"); v.add("nsstudent"); v.add("."); v.add("com"); // Find the size of Vector element System.out.println("There are "+v.size() +" Element in this list "); //Display Vector element using iterator method . // enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) { System.out.print(vEnum.nextElement() + " "); } System.out.println(); //Display Vector element using iterator method . Iterator iter = v.iterator(); System.out.println("---Element of Vector --- "); while (iter.hasNext()) { System.out.print(iter.next() + " "); } System.out.println(); // Display Vector element using size() and get() System.out.println("---Element of Vector --- "); for (int i = 0; i< v.size(); i++) { System.out.print(v.get(i) + " , "); } System.out.println(); // Display Vector element using Iterator method ListIterator listIter = v.listIterator(); System.out.println("---Element of Vector --- "); while (listIter.hasNext()) { System.out.print(listIter.next() + " , "); } System.out.println(); } }
Output


There are 5 Element in this list

Elements in vector:
Welcome to nsstudent . com
---Element of Vector ---
Welcome to nsstudent . com
---Element of Vector ---
Welcome , to , nsstudent , . , com ,
---Element of Vector ---
Welcome , to , nsstudent , . , com ,

Download this example
 



 

Latest Tutorial


Most Visited Tutorial