标签:
(Referrence: TutorialsPoint)
Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time.
Examples:
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.List; 4 5 public class ArrayListLoopingExample { 6 public static void main(String[] args) { 7 8 List<String> list = new ArrayList<String>(); 9 list.add("Text 1"); 10 list.add("Text 2"); 11 list.add("Text 3"); 12 13 Iterator<String> iterator = list.iterator(); 14 while (iterator.hasNext()) { 15 System.out.println(iterator.next()); 16 } 17 } 18 }
标签:
原文地址:http://www.cnblogs.com/ireneyanglan/p/4899757.html