Java : Vector Example


 

Java : Vector Example

This segment of tutorial illustrates about the Vector class and its use with the Iterator interface.

This segment of tutorial illustrates about the Vector class and its use with the Iterator interface.

Java : Vector Example

This segment of tutorial illustrates about the Vector class and its use with the Iterator interface.

Java Vector :

Vector class implements dynamic array of objects. Like array you can access its component by using its index.
It is similar to ArrayList but some properties make it different to it as -Vector is synchronized and it contain many methods that are not part of collections.

Example :

import java.util.Iterator;
import java.util.Vector;

class VectorExample {
public static void main(String[] args) {
Vector v = new Vector();

// Add elements to the vector
v.add(400);
v.add(240);
v.add(600);
v.add(555);

Iterator it = v.iterator();
System.out.println("Vector elements : ");
for (; it.hasNext();) {
System.out.println(it.next());
}

}
}

Description : In this example we are using vector to store elements and then by using iterator we are printing back to console.
You can create vector as -
Vector v = new Vector();
Here add() method is used for adding element in vector v. Then by using Iterator interface we are displaying the added elements of vector.

Output :

Vector elements : 
400
240
600
555 

 

Ads