Vector in java
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. Using the Vector class, rather than creating your own linked-list structure is a good idea. Vectors are extremely easy to use, and implement the Enumeration interface which makes traversing the contents of a Vector extremely easy.
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation. Creating a vector is easy. Simply define a variable of type Vector, and call the vector constructor.
Vector instances, like linked-lists, can grow dynamically. Once the vector reaches its maximum limit, it dynamically allocated resources and increases its maximum. However, if you know you're going to insert a large amount of data, you may want to specify an initial estimate.
Vector instances can store any type of object - you simply insert an object via the addElement method.
When you wish to access the elements of the vector, you can look at individual elements (via their offset within the vector which is in ascending order), or you can traverse the entire list.
Traversing the entire vector is a common opperation. Remember, however, that when you insert an object via the addElement, it is implicitly cast to an instance of Object. Object is the base class of all other Java classes, and you'll need to explicitly cast it to another object type.
Code Example:
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation. Creating a vector is easy. Simply define a variable of type Vector, and call the vector constructor.
// Create an instance of class Vector ....
Vector myVector = new Vector
();
// ... or suggest an initial vector
size
myVector = new
Vector(50);
Vector instances can store any type of object - you simply insert an object via the addElement method.
for (int i = 1; i <=
20; i++)
{
myVector.addElement(
new String ("I am
string " + i));
}
When you wish to access the elements of the vector, you can look at individual elements (via their offset within the vector which is in ascending order), or you can traverse the entire list.
// Peek at element 5
System.out.println
("Element : " + myVector.elementAt(5)
);
Traversing the entire vector is a common opperation. Remember, however, that when you insert an object via the addElement, it is implicitly cast to an instance of Object. Object is the base class of all other Java classes, and you'll need to explicitly cast it to another object type.
// Traverse entire list, printing them all out using while loop
Enumeration e=vector.elements();
System.out.println("The
elements of vector: " + vector);
while(e.hasMoreElements())
{
System.out.println("The
elements are: " + e.nextElement());
}
// Traverse entire list, using for loop
for (Enumeration e = myVector.elements();
e.hasMoreElements(); )
{
String
myString = (String) e.nextElement();
System.out.println(myString);
}
Traversing a list is made extremely simple through the use of the Enumeration interface. Enumerations are great, because they allow you to easily process all elements, without having to determine the length of the vector, and manually accessing each element. Instead, it returns all the elements through the nextElement method.
Some useful methods of Vector class are given below.
addElement(Object o) or add(Object o): Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.
add(int index,Object o) : Inserts the specified element at the specified position in this Vector. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
size(): It gives the number of element in the vector.
capacity(): The current capacity (the length of its internal data array, kept in the field elementData of this vector)
elementAt(int index): It returns the element at the specified index.
firstElement(): It returns the first element of the vector.
lastElement(): It returns last element.
removeElementAt(int index): Deletes the component at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously. The size of this vector is decreased by 1.
elements(): It returns an enumeration of the element
In this example we have also used Enumeration interface to retrieve the value of a vector. Enumeration interface has two methods.
hasMoreElements(): It checks if this enumeration contains more elements or not. nextElement(): It checks the next element of the enumeration.
Code Example:
package
aurangzeb.utk.com;
import
java.util.Enumeration;
import
java.util.Vector;
public class VectorDemo {
public static void main(String[]
args) {
Vector<Object>
vector = new Vector<Object>();
int primitiveType =
10;
Integer
wrapperType = new Integer(20);
String
str = "aurangzeb khan";
vector.add(primitiveType);
vector.add(wrapperType);
vector.add(str);
vector.add(2,
new Integer(30));
System.out.println("the elements
of vector: " + vector);
System.out.println("The size
of vector are: " + vector.size());
System.out.println("The
elements at position 2 is: "
+
vector.elementAt(2));
System.out.println("The
first element of vector is: "
+
vector.firstElement());
System.out.println("The last
element of vector is: "
+
vector.lastElement());
vector.removeElementAt(2);
Enumeration
e = vector.elements();
System.out.println("The
elements of vector: " + vector);
while (e.hasMoreElements())
{
System.out.println("The
elements are: " + e.nextElement());
}
}
}
Output of the above code:
the elements of vector: [10, 20, 30, aurangzeb khan]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: aurangzeb khan
The elements of vector: [10, 20, aurangzeb khan]
The elements are: 10
The elements are: 20
The elements are: aurangzeb khan
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: aurangzeb khan
The elements of vector: [10, 20, aurangzeb khan]
The elements are: 10
The elements are: 20
The elements are: aurangzeb khan
