Home Tutorial Java Core Java : Vector Example

 
 

Share on Google+Share on Google+
Java : Vector Example
Posted on: November 20, 2012 at 12:00 AM
Advertisement
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 

 

Advertisement

Related Tags for Java : Vector Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com