We can read all elements in vector by following ways:
- Read all elements in vector using iterator.
- Read all elements in vector using Enumeration.
Read all elements in vector using iterator
package com.w3schools;
import java.util.Iterator;
import java.util.Vector;
/**
* Read all elements in vector using iterator
* @author w3schools
*/
public class ReadVectorElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//Adding elements to the end
vector.add("Jai");
vector.add("Mahesh");
vector.add("Hemant");
vector.add("Vishal");
Iterator<String> iterator = vector.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
} |
package com.w3schools;
import java.util.Iterator;
import java.util.Vector;
/**
* Read all elements in vector using iterator
* @author w3schools
*/
public class ReadVectorElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//Adding elements to the end
vector.add("Jai");
vector.add("Mahesh");
vector.add("Hemant");
vector.add("Vishal");
Iterator<String> iterator = vector.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output
Read all elements in vector using Enumeration
package com.w3schools;
import java.util.Enumeration;
import java.util.Vector;
/**
* Read all elements in vector using Enumeration
* @author w3schools
*/
public class ReadVectorElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//Adding elements to the end
vector.add("Jai");
vector.add("Mahesh");
vector.add("Hemant");
vector.add("Vishal");
Enumeration<String> enumeration = vector.elements();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
} |
package com.w3schools;
import java.util.Enumeration;
import java.util.Vector;
/**
* Read all elements in vector using Enumeration
* @author w3schools
*/
public class ReadVectorElements {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//Adding elements to the end
vector.add("Jai");
vector.add("Mahesh");
vector.add("Hemant");
vector.add("Vishal");
Enumeration<String> enumeration = vector.elements();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
}
Output