Java教程

Java Vector iterator()

Java Vector类的 iterator()方法用于获取该列表中元素的迭代器。

语法

以下是 iterator()方法的声明:
public Iterator<E> iterator()

参数

此方法不接受任何参数。

返回

迭代器()方法以适当的顺序返回此列表中元素的迭代器。

异常

NA

兼容性版本

Java 1.2及更高版本

示例1

import java.util.*;
public class VectorIteratorExample1 {  
    public static void main(String arg[]) {   
        //Create an empty Vector      
        Vector < String > colors = new Vector < String > ();
        //Add color elements in the vector
            colors.add("Red");
            colors.add("Green");
            colors.add("Blue");
            colors.add("Pink");
            //Obtain an Iterator
            Iterator<String> itr = colors.iterator();
            while(itr.hasNext()){
                 System.out.println(itr.next());
            } 
    }            
}
输出:
Red
Green
Blue
Pink

示例2

import java.util.*;
public class VectorIteratorExample2 {  
    public static void main(String arg[]) {   
       //Create an empty Vector      
       Vector < Integer > in = new Vector < > ();
       //Add elements in the vector
       in.add(100);
         in.add(200);
       in.add(300);
       in.add(400);
        //Obtain an Iterator
        Iterator<Integer> itr = in.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());
        } 
    }            
}
输出:
100
200
300
400

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4