Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - The Vector Class



Vector implements a dynamic array. It is similar to ArrayList, but with two differences −

  • Vector is synchronized.

  • Vector contains many legacy methods that are not part of the collections framework.

Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.

Following is the list of constructors provided by the vector class.

Sr.No. Constructor & Description
1

Vector( )

This constructor creates a default vector, which has an initial size of 10.

2

Vector(int size)

This constructor accepts an argument that equals to the required size, and creates a vector whose initial capacity is specified by size.

3

Vector(int size, int incr)

This constructor creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward.

4

Vector(Collection c)

This constructor creates a vector that contains the elements of collection c.

Apart from the methods inherited from its parent classes, Vector defines the following methods −

Sr.No. Method & Description
1 boolean add(E e)

This method appends the specified element to the end of this Vector.

2 boolean addAll(Collection<? extends E> c)

This method appends all of the elements in the specified Collection to the end of this Vector.

3 void addElement(E obj)

This method adds the specified component to the end of this vector, increasing its size by one.

4 int capacity()

This method returns the current capacity of this vector.

5 void clear()

This method removes all of the elements from this vector.

6 clone clone()

This method returns a clone of this vector.

7 boolean contains(Object o)

This method returns true if this vector contains the specified element.

8 boolean containsAll(Collection<?> c)

This method returns true if this Vector contains all of the elements in the specified Collection.

9 void copyInto(Object[ ] anArray)

This method copies the components of this vector into the specified array.

10 E elementAt(int index)

This method returns the component at the specified index.

11 Enumeration<E> elements()

This method returns an enumeration of the components of this vector.

12 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.

13 boolean equals(Object o)

This method compares the specified Object with this Vector for equality.

14 E firstElement()

This method returns the first component (the item at index 0) of this vector.

15 void forEach​(Consumer<? super E> action)

This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

16 E get(int index)

This method returns the element at the specified position in this Vector.

17 int hashCode()

This method returns the hash code value for this Vector.

18 int indexOf(Object o)

This method returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

19 void insertElementAt(E obj, int index)

This method inserts the specified object as a component in this vector at the specified index.

20 boolean isEmpty()

This method tests if this vector has no components.

21 Iterator<E&t; iterator()

This method returns an iterator over the elements in this list in proper sequence.

22 E lastElement()

This method returns the last component of the vector.

23 int lastIndexOf​(Object o)

This method returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

24 ListIterator<E> listIterator()

This method returns a list iterator over the elements in this list (in proper sequence).

25 E remove(int index)

This method removes the element at the specified position in this Vector.

26 boolean removeAll(Collection<?> c)

This method removes from this Vector all of its elements that are contained in the specified Collection.

27 void removeAllElements()

This method removes all components from this vector and sets its size to zero.

28 boolean removeElement(Object obj)

This method removes the first occurrence of the argument from this vector.

29 void removeElementAt(int index)

This method deletes the component at the specified index.

30 boolean removeIf​(Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate.

31 boolean retainAll(Collection<?> c)

This method retains only the elements in this Vector that are contained in the specified Collection.

32 E set(int index, E element)

This method replaces the element at the specified position in this Vector with the specified element.

33 void setElementAt(E obj, int index)

This method sets the component at the specified index of this vector to be the specified object.

34 void setSize(int newSize)

This method sets the size of this vector.

35 int size()

This method returns the number of components in this vector.

36 Spliterator<E&t; spliterator()

Creates a late-binding and fail-fast Spliterator over the elements in this list.

37 List <E> subList(int fromIndex, int toIndex)

This method returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.

38 object[] toArray()

This method returns an array containing all of the elements in this Vector in the correct order.

39 String toString()

This method returns a string representation of this Vector, containing the String representation of each element.

40 void trimToSize()

This method trims the capacity of this vector to be the vector's current size.

Example

The following program illustrates several of the methods supported by this collection −

import java.util.*;
public class VectorDemo {

   public static void main(String args[]) {
      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);
      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " + v.capacity());
      
      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));
      System.out.println("Capacity after four additions: " + v.capacity());

      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));
      System.out.println("Current capacity: " + v.capacity());
      
      v.addElement(new Integer(11));
      v.addElement(new Integer(12));
      System.out.println("First element: " + (Integer)v.firstElement());
      System.out.println("Last element: " + (Integer)v.lastElement());
      
      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");
         
      // 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();
   }
}

This will produce the following result −

Output

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12
java_data_structures.htm
Advertisements