集册 Java实例教程 使用迭代器遍历元素向量

使用迭代器遍历元素向量

欢马劈雪     最近更新时间:2020-01-02 10:19:05

381
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用Iterator遍历元素Vector


import java.util.Vector;
/** 
来 自 
n o w    j a v a  . c o m
**/

import java.util.Iterator;

 

public class Main {

 

  public static void main(String[] args) {

    Vector v = new Vector();

   

    v.add("1");

    v.add("2");

    v.add("3");

    v.add("4");

    v.add("5");

   

    //get an Iterator object for Vector using iterator() method.

    Iterator itr = v.iterator();

   

    //use hasNext() and next() methods of Iterator to iterate through the elements//n o w  j a v a  . c o m 提供

    while(itr.hasNext())

      System.out.println(itr.next());

  }

}