Introduction
The Vector class implements a growable array (dynamic array). In the vector class, a component is stored at an integer index and it can be accessed by this index number like an Array. A Vector is synchronized and it contains many legacy methods that are not part of the collection framework. As of Java1.2, the Vector class has been retrofitted to implement a List.
Hierarchy of vector class
Constructor Summary
- Vector(): Creates an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
- Vector(Collection<? extends E> c): Creates a vector containing the elements of the specified collection, in the order, they are returned by the collection's iterator.
- Vector(int initialCapacity): Creates an empty vector with the specified initial capacity and with its capacity increment equal to zero.
- Vector(int initialCapacity, int capacityIncrement): Creates an empty vector with the specified initial capacity and capacity increment.
Method Summary
Methods for Add elements to this vector: The following methods are used to add an element of the vector class.
Return type |
Method name |
Description of functionality |
boolean |
add(E e) |
Appends the specified element to the end of this Vector. |
void |
add(int index, E element) |
Inserts the specified element at the specified position in this Vector. |
boolean |
addAll(Collection<? extends E> c) |
Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator. |
boolean |
addAll(int index, Collection<? extends E> c) |
Inserts all of the elements in the specified Collection into this Vector at the specified position. |
void |
addElement(E obj) |
Adds the specified component to the end of this vector, increasing its size by one. |
void |
insertElementAt(E obj, int index) |
Inserts the specified object as a component in this vector at the specified index. |
Methods for Size and capacity: The following methods are used to determine the capacity and size of the vector.
Return type |
Method name |
Description of functionality |
int |
capacity() |
Returns the current capacity of this vector. |
void |
ensureCapacity(int minCapacity) |
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. |
void |
setSize(int newSize) |
Sets the size of this vector. |
int |
size() |
Returns the number of components in this vector. |
void |
trimToSize() |
Trims the capacity of this vector to be the vector's current size. |
Methods for Remove all elements from a vector: Used for removing all the elements of the vector.
Return type |
Method name |
Description of functionality |
void |
clear() |
Removes all of the elements from this Vector. |
Clone a vector: This method is used for making an object clone of the vector class.
Return type |
Method name |
Description of functionality |
Object |
clone() |
Returns a clone of this vector |
Methods for check contain a certain element: These methods are for checking whether elements are present or not.
Return type |
Method name |
Description of functionality |
boolean |
contains(Object o) |
Returns true if this vector contains the specified element. |
boolean |
containsAll(Collection<?> c) |
Returns true if this Vector contains all of the elements in the specified Collection. |
Methods for Copy elements: This method is used to copy elements of a vector into an array.
Return type |
Method name |
Description of functionality |
void |
copyInto(Object[] anArray) |
Copies the components of this vector into the specified array. |
Methods for Get Enumeration from this vector: This method is for getting an enumeration object from a vector class.
Return type |
Method name |
Description of functionality |
Enumeration<E> |
elements() |
Returns an enumeration of the components of this vector. |
Methods for Compare two vectors: This method is used to compare the elements of two vector class objects.
Return type |
Method name |
Description of functionality |
boolean |
equals(Object o) |
Compares the specified Object with this Vector for equality. |
Methods for Get element from vector: Using are following methods we can get the elements of a vector object as needed.
Return type |
Method name |
Description of functionality |
E |
elementAt(int index) |
Returns the component at the specified index |
E |
firstElement() |
Returns the first component (the item at index 0) of this vector. |
E |
get(int index) |
Returns the element at the specified position in this Vector |
E |
lastElement() |
Returns the last component of the vector. |
Methods for check is vector empty: This method is used to check whether a vector is empty or not.
Return type |
Method name |
Description of functionality |
boolean |
isEmpty() |
Tests if this vector has no components. |
Methods for Get element index: These methods are useful for finding the indexing position of an element.
Return type |
Method name |
Description of functionality |
int |
indexOf(Object o) |
Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
int |
indexOf(Object o, int index) |
Returns the index of the first occurrence of the specified element in this vector, searching forwards from the index, or returns -1 if the element is not found. |
int |
lastIndexOf(Object o) |
Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. |
int |
lastIndexOf(Object o, int index) |
Returns the index of the last occurrence of the specified element in this vector, searching backward from the index, or returns -1 if the element is not found. |
Methods for Remove element from a vector.
Return type |
Method name |
Description of functionality |
E |
remove(int index) |
Removes the element at the specified position in this Vector. |
boolean |
remove(Object o) |
Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged. |
boolean |
removeAll(Collection<?> c) |
Removes from this Vector all of its elements that are contained in the specified Collection. |
void |
removeAllElements() |
Removes all components from this vector and sets its size to zero. |
boolean |
removeElement(Object obj) |
Removes the first (lowest-indexed) occurrence of the argument from this vector. |
void |
removeElementAt(int index) |
Deletes the component at the specified index |
Methods for Retain elements collection c has.
Return type |
Method name |
Description of functionality |
boolean |
retainAll(Collection<?> c) |
Retains only the elements in this Vector that are contained in the specified Collection. |
Methods for Replace element in a vector.
Return type |
Method name |
Description of functionality |
E |
set(int index, E element) |
Replaces the element at the specified position in this Vector with the specified element. |
void |
setElementAt(E obj, int index) |
Sets the component at the specified index of this vector to be the specified object. |
Methods for Get a sub list from a list.
Return type |
Method name |
Description of functionality |
List<E> |
subList(int fromIndex, int toIndex) |
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
16-Converts Vector to Array.
Return type |
Method name |
Description of functionality |
Object[] |
toArray() |
Returns an array containing all of the elements in this Vector in the correct order. |
<T> T[] |
toArray(T[] a) |
Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array. |
String |
toString() |
Returns a string representation of this Vector, containing the String representation of each element. |
Example
First look at the code of this program carefully and read the used method detail, which is given in an appropriate table and after this, you can understand the following output very easily.
- import java.util.*;
- class VectorClassDemo {
- public static void main(String args[]) {
-
- Vector v = new Vector(5, 3);
-
- System.out.println("Initial size: " + v.size());
-
- System.out.println("Initial capacity: " +
- v.capacity());
-
- v.addElement(new Integer(10));
- v.addElement(new Integer(11));
- v.addElement(new Integer(12));
- v.addElement(new Integer(13));
- v.addElement(new Integer(14));
- System.out.println("After inserting five element the capacity is : " +
- v.capacity());
-
- v.addElement(new Double(17.045));
- System.out.println("now capacity: " +
- v.capacity());
- v.addElement(new Double(3.08));
- v.addElement(new Integer(12));
- System.out.println("now capacity: " +
- v.capacity());
- v.addElement(new Float(7.4));
- v.addElement(new Integer(10));
- System.out.println("after inserting 3 more element so now capacity: " +
- v.capacity());
- v.addElement(new Integer(18));
- v.addElement(new Integer(19));
-
- 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.");
-
- Enumeration venumeration = v.elements();
- System.out.println("\nElements in vector:");
- while (venumeration.hasMoreElements())
- System.out.print(venumeration.nextElement() + "-->");
- System.out.println();
- }
- }
OUTPUT