javase-常用三种遍历方法
javase-常用三种遍历方法
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.List; 4 public class Bianli { 5 public static void main(String[] args) { 6 Listlist = new ArrayList (); 7 list.add("add"); 8 list.add("bdd"); 9 list.add("cdd"); 10 // 三种遍历方式 for();for()增强;iterator 11 for (int i = 0; i < list.size(); i++) { 12 String s = list.get(i); 13 System.out.println(s); 14 } 15 for (String string : list) { 16 System.out.println(string); 17 } 18 Iterator iterator = list.iterator(); 19 while(iterator.hasNext()){ 20 System.out.println(iterator.next()); 21 } 22 } 23 }