Java Program to Implement Vector API

This Java program Implements Vector API.The Vector class implements a growable array of objects. 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 Vector has been created.

Here is the source code of the Java Program to Implement Vector API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.Enumeration;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6. import java.util.Vector;
  7.  
  8. public class VectorImpl<E>
  9. {
  10.     private Vector<E> vector;
  11.  
  12.     /**
  13.        Constructs an empty vector so that its internal data array has size 10
  14.        and its standard capacity increment is zero.
  15.     **/
  16.     public VectorImpl()
  17.     {
  18.         vector = new Vector<E>();
  19.     }
  20.  
  21.     /**
  22.        Constructs an empty vector so that its internal data array has size 10
  23.        and its standard capacity increment is zero.
  24.     **/
  25.     public VectorImpl(Collection<? extends E> c)
  26.     {
  27.         vector = new Vector<E>(c);
  28.     }
  29.  
  30.     /**
  31.        Constructs an empty vector with the specified initial capacity and with
  32.        its capacity increment equal to zero
  33.     **/
  34.     public VectorImpl(int initialCapacity)
  35.     {
  36.         vector = new Vector<E>(initialCapacity);
  37.     }
  38.  
  39.     /**
  40.        Constructs an empty vector with the specified initial capacity and
  41.        capacity increment.
  42.     **/
  43.     public VectorImpl(int initialCapacity, int capacityIncrement)
  44.     {
  45.         vector = new Vector<E>(initialCapacity, capacityIncrement);
  46.     }
  47.  
  48.     /** Appends the specified element to the end of this Vector. **/
  49.     public boolean add(E e)
  50.     {
  51.         return vector.add(e);
  52.     }
  53.  
  54.     /** Inserts the specified element at the specified position in this Vector. **/
  55.     public void add(int index, E element)
  56.     {
  57.         vector.add(index, element);
  58.     }
  59.  
  60.     /** 
  61.        Appends all of the elements in the specified Collection to the end of
  62.        this Vector, in the order that they are returned by the specified
  63.        Collection's Iterator.
  64.     **/
  65.     public boolean addAll(Collection<? extends E> c)
  66.     {
  67.         return vector.addAll(c);
  68.     }
  69.  
  70.     /**
  71.        Inserts all of the elements in the specified Collection into this Vector
  72.        at the specified position.
  73.     **/
  74.     public boolean addAll(int index, Collection<? extends E> c)
  75.     {
  76.         return vector.addAll(index, c);
  77.     }
  78.  
  79.     /** Adds the specified component to the end of this vector, increasing its size by one. **/
  80.     public void addElement(E obj)
  81.     {
  82.         vector.addElement(obj);
  83.     }
  84.  
  85.     /** Returns the current capacity of this vector. **/
  86.     public int capacity()
  87.     {
  88.         return vector.capacity();
  89.     }
  90.  
  91.     /** Removes all of the elements from this Vector. **/
  92.     public void clear()
  93.     {
  94.         vector.clear();
  95.     }
  96.  
  97.     /** Returns a clone of this vector. **/
  98.     public Object clone()
  99.     {
  100.         return vector.clone();
  101.     }
  102.  
  103.     /** Returns true if this vector contains the specified element. **/
  104.     public boolean contains(Object o)
  105.     {
  106.         return vector.contains(o);
  107.     }
  108.  
  109.     /** Returns true if this Vector contains all of the elements in the specified Collection. **/
  110.     public boolean containsAll(Collection<?> c)
  111.     {
  112.         return vector.containsAll(c);
  113.     }
  114.  
  115.     /** Copies the components of this vector into the specified array. **/
  116.     public void copyInto(Object[] anArray)
  117.     {
  118.         vector.copyInto(anArray);
  119.     }
  120.  
  121.     /** Returns the component at the specified index. **/
  122.     public E elementAt(int index)
  123.     {
  124.         return vector.elementAt(index);
  125.     }
  126.  
  127.     /** Returns an enumeration of the components of this vector. **/
  128.     public Enumeration<E> elements()
  129.     {
  130.         return vector.elements();
  131.     }
  132.  
  133.     /**
  134.        Increases the capacity of this vector, if necessary, to ensure that it
  135.        can hold at least the number of components specified by the minimum
  136.        capacity argument.
  137.     **/
  138.     public void ensureCapacity(int minCapacity)
  139.     {
  140.         vector.ensureCapacity(minCapacity);
  141.     }
  142.  
  143.     /** Compares the specified Object with this Vector for equality. **/
  144.     public boolean equals(Object o)
  145.     {
  146.         return vector.equals(o);
  147.     }
  148.  
  149.     /** Returns the first component (the item at index 0) of this vector. **/
  150.     public E firstElement()
  151.     {
  152.         return vector.firstElement();
  153.     }
  154.  
  155.     /** Returns the element at the specified position in this Vector. **/
  156.     public E get(int index)
  157.     {
  158.         return vector.get(index);
  159.     }
  160.  
  161.     /** Returns the hash code value for this Vector. **/
  162.     public int hashCode()
  163.     {
  164.         return vector.hashCode();
  165.     }
  166.  
  167.     /**
  168.        Returns the index of the first occurrence of the specified element in
  169.        this vector, or -1 if this vector does not contain the element.
  170.     **/
  171.     public int indexOf(Object o)
  172.     {
  173.         return vector.indexOf(o);
  174.     }
  175.  
  176.     /**
  177.        Returns the index of the last occurrence of the specified element in this
  178.        vector, searching backwards from index, or returns -1 if the element is
  179.        not found.
  180.     **/
  181.     public int indexOf(Object o, int index)
  182.     {
  183.         return vector.indexOf(o, index);
  184.     }
  185.  
  186.     /** Inserts the specified object as a component in this vector at the specified index. **/
  187.     public void insertElementAt(E obj, int index)
  188.     {
  189.         vector.insertElementAt(obj, index);
  190.     }
  191.  
  192.     /** Tests if this vector has no components. **/
  193.     public boolean isEmpty()
  194.     {
  195.         return vector.isEmpty();
  196.     }
  197.  
  198.     /** Returns an iterator over the elements in this list in proper sequence. **/
  199.     public Iterator<E> iterator()
  200.     {
  201.         return vector.iterator();
  202.     }
  203.  
  204.     /** Returns the last component of the vector. **/
  205.     public E lastElement()
  206.     {
  207.         return vector.lastElement();
  208.     }
  209.  
  210.     /**
  211.        Returns the index of the last occurrence of the specified element in this
  212.        vector, or -1 if this vector does not contain the element.
  213.     **/
  214.     public int lastIndexOf(Object o)
  215.     {
  216.         return vector.lastIndexOf(o);
  217.     }
  218.  
  219.     /**
  220.        Returns the index of the last occurrence of the specified element in this
  221.        vector, searching backwards from index, or returns -1 if the element is
  222.        not found.
  223.     **/
  224.     public int lastIndexOf(Object o, int index)
  225.     {
  226.         return vector.lastIndexOf(o, index);
  227.     }
  228.  
  229.     /**  Returns a list iterator over the elements in this list (in proper sequence). **/
  230.     public ListIterator<E> listIterator()
  231.     {
  232.         return vector.listIterator();
  233.     }
  234.  
  235.     /**
  236.        Returns a list iterator over the elements in this list (in proper sequence),
  237.        starting at the specified position in the list.
  238.     **/
  239.     public ListIterator<E> listIterator(int index)
  240.     {
  241.         return vector.listIterator(index);
  242.     }
  243.  
  244.     /** Removes the element at the specified position in this Vector. **/
  245.     public E remove(int index)
  246.     {
  247.         return vector.remove(index);
  248.     }
  249.  
  250.     /** 
  251.        Removes the first occurrence of the specified element in this Vector If
  252.        the Vector does not contain the element, it is unchanged.
  253.     **/
  254.     public boolean remove(Object o)
  255.     {
  256.         return vector.remove(o);
  257.     }
  258.  
  259.     /**
  260.        Removes from this Vector all of its elements that are contained in the
  261.        specified Collection.
  262.     **/
  263.     public boolean removeAll(Collection<?> c)
  264.     {
  265.         return vector.removeAll(c);
  266.     }
  267.  
  268.     /** Removes all components from this vector and sets its size to zero. **/
  269.     public void removeAllElements()
  270.     {
  271.         vector.removeAllElements();
  272.     }
  273.  
  274.     /** Removes the first (lowest-indexed) occurrence of the argument from this vector. **/
  275.     public boolean removeElement(Object obj)
  276.     {
  277.         return vector.removeElement(obj);
  278.     }
  279.  
  280.     /** Retains only the elements in this Vector that are contained in specified Collection. **/
  281.     public boolean retainAll(Collection<?> c)
  282.     {
  283.         return vector.removeAll(c);
  284.     }
  285.  
  286.     /** Replaces the element at the specified position in this Vector with the specified element. **/
  287.     public E set(int index, E element)
  288.     {
  289.         return vector.set(index, element);
  290.     }
  291.  
  292.     /** Sets the component at the specified index of this vector to be the specified object. **/
  293.     public void setElementAt(E obj, int index)
  294.     {
  295.         vector.setElementAt(obj, index);
  296.     }
  297.  
  298.     /** Sets the size of this vector. **/
  299.     public void setSize(int newSize)
  300.     {
  301.         vector.setSize(newSize);
  302.     }
  303.  
  304.     /** Returns the number of components in this vector. **/
  305.     public int size()
  306.     {
  307.         return vector.size();
  308.     }
  309.  
  310.     /** Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. **/
  311.     public List<E> subList(int fromIndex, int toIndex)
  312.     {
  313.         return vector.subList(fromIndex, toIndex);
  314.     }
  315.  
  316.     /** Trims the capacity of this vector to be the vector's current size. **/
  317.     public void trimToSize()
  318.     {
  319.         vector.trimToSize();
  320.     }
  321.  
  322.     public static void main(String... arg)
  323.     {
  324.         VectorImpl<Integer> vector = new VectorImpl<Integer>();
  325.         vector.add(100);
  326.         vector.add(200);
  327.         vector.add(399);
  328.         vector.add(120);
  329.         vector.addElement(400);
  330.         vector.addElement(40);
  331.         System.out.println("the capacity of the vector is " + vector.capacity());
  332.         System.out.println("elements of vector is ");
  333.         Enumeration<Integer> elements = vector.elements();
  334.         while (elements.hasMoreElements())
  335.         {
  336.             System.out.print(elements.nextElement() + "\t");
  337.         }
  338.         System.out.println();
  339.         if (vector.contains(200))
  340.             System.out.println("the vector contains 200");
  341.         System.out.println("the first element is " + vector.firstElement());
  342.         System.out.println("the last element is  " + vector.lastElement());
  343.         System.out.println("element " + vector.remove(2) + " at index 2 removed"); 
  344.         System.out.println("the size of the vector is " + vector.size());
  345.     }
  346. }

$ javac VectorImpl.java
$ java VectorImpl
the capacity of the vector is 10
elements of vector is 
100	200	399	120	400	40	
the vector contains 200
the first element is 100
the last element is  40
element 399 at index 2 removed
the size of the vector is 5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.