CodesJava

Easy learning with example program codes

Read all elements in vector

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.codesjava;
 
import java.util.Iterator;
import java.util.Vector;
 
/**
 * Read all elements in vector using iterator
 * @author codesjava
 */
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

Jai
Mahesh
Hemant
Vishal

Read all elements in vector using Enumeration

package com.codesjava;
 
import java.util.Enumeration;
import java.util.Vector;
 
/**
 * Read all elements in vector using Enumeration
 * @author codesjava
 */
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

Jai
Mahesh
Hemant
Vishal
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status