让我们首先创建一个List并添加元素-
List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" });
现在,使用Iterator来获取每个元素-
Iterator<String>i = list.iterator();
显示元素-
while (i.hasNext()) { System.out.println(i.next()); }
import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] a) { List<String>list = Arrays.asList(new String[] { "P", "Q", "R", "S", "T", "U", "V", "W" }); Iterator<String>i = list.iterator(); System.out.println("Displaying elements..."); while (i.hasNext()) { System.out.println(i.next()); } } }
输出结果
Displaying elements... P Q R S T U V W