Latest web development tutorials

Java Vector class

Vector class implements a dynamic array. And ArrayList and similar, but they are different:

  • Vector is synchronized access.
  • Vector contains many of the traditional methods, which do not belong to the set framework.

Vector is mainly used in the size of the array is not known beforehand, or just need someone to change the situation of the array size.

Vector class supports four constructor.

The first constructor creates a default vector, the default size is 10:

Vector()

The second constructor creates a vector of the specified size.

Vector(int size)

The third constructor creates a vector of the specified size, and the increment specified by incr. Increments the number of vector elements in increments of.

Vector(int size,int incr)

Fourth constructor creates a vector that contains a collection of c elements:

Vector(Collection c)

In addition to the parent class method inherited from outer Vector also defines the following methods:

No. Method Description
1 void add (int index, Object element)
At the specified position in this Vector Inserts the specified element.
2 boolean add (Object o)
Adds the specified element to the end of the vector.
3 boolean addAll (Collection c)
Collection will be specified in all of the elements added to the end of this vector, these elements are added in the order specified collection's iterator returned.
4 boolean addAll (int index, Collection c)
At the specified location in the specified Collection into this Vector all of the elements.
5 void addElement (Object obj)
The specified component to the end of this vector, increasing its size by one.
6 int capacity ()
Returns the current capacity of this vector.
7 void clear ()
Removes all of the elements from this Vector.
8 Object clone ()
Returns a copy of the vector.
9 boolean contains (Object elem)
If this vector contains the specified elements, it returns true.
10 boolean containsAll (Collection c)
If this Vector contains all of the elements in the specified Collection, it returns true.
11 void copyInto (Object [] anArray)
Copy the components of this vector into the specified array.
12 Object elementAt (int index)
Returns the component at the specified index.
13 Enumeration elements ()
Returns an enumeration of the components of this vector.
14 void ensureCapacity (int minCapacity)
Increase 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)
Compares the specified object with this Vector for equality.
16 Object firstElement ()
Returns the first component of the vector (at index 0) item at).
17 Object get (int index)
Returns the vector element at the specified location.
18 int hashCode ()
Returns the hash code value for this Vector.
19 int indexOf (Object elem)
Returns the index of this vector, the first occurrence of the specified element, if this vector does not contain the element, it returns -1.
20 int indexOf (Object elem, int index)
Returns the index of this vector, the first occurrence of the specified element, searching forwards from index, if the element is not found, it returns -1.
twenty one void insertElementAt (Object obj, int index)
The specified object as a component in this vector into the specified index.
twenty two boolean isEmpty ()
Tests if this vector has no components.
twenty three Object lastElement ()
Returns the last component of this vector.
twenty four int lastIndexOf (Object elem)
Returns the index of this vector, the last occurrence of the specified element; if this vector does not contain the element, it returns -1.
25 int lastIndexOf (Object elem, int index)
Returns index of the specified element in this vector, the last occurrence, searching backwards from index, if the element is not found, it returns -1.
26 Object remove (int index)
Remove the specified position in this Vector elements.
27 boolean remove (Object o)
Remove the specified element in this vector the first match, if the vector does not contain the element, it is unchanged.
28 boolean removeAll (Collection c)
Removes from this Vector contains all of the elements in the specified Collection.
29 void removeAllElements ()
Removes all components from this vector and sets its size to zero.
30 boolean removeElement (Object obj)
Removes the variable vector in the first (lowest-indexed) occurrence.
31 void removeElementAt (int index)
Removes the specified component index.
32 protected void removeRange (int fromIndex, int toIndex)
Removes whose index is between fromIndex (inclusive) and all elements toIndex (not included) between List.
33 boolean retainAll (Collection c)
In this vector elements in the specified Collection contains only reservations.
34 Object set (int index, Object element)
Replace the elements in this Vector with the specified element at the specified position.
35 void setElementAt (Object obj, int index)
This component at the specified index vector to the specified object.
36 void setSize (int newSize)
Set the size of this vector.
37 int size ()
Returns the number of components in this vector.
38 List subList (int fromIndex, int toIndex)
Back view of the portion of this List, the range of elements from fromIndex (inclusive) to toIndex (not included).
39 Object [] toArray ()
It returns an array containing all of the elements in this Vector in the correct order.
40 Object [] toArray (Object [] a)
It returns an array containing all of the elements in this Vector in the correct order; the type of returned array when the type of the specified array.
41 String toString ()
Returns a string representation of this Vector, containing the String representation of each element.
42 void trimToSize ()
Fine-tune the capacity of this vector, the vector's current size.

Examples

The following program illustrates this collection supports several methods:

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();
   }
}

The above examples compiled results are as follows:

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