【Java】List的三种遍历方法
1 public void List_Test(){ 2 List<String>list = new ArrayList<String>(); 3 for(int i = 0;i < 10;i++){ 4 list.add("Hey" + i); 5 } 6 7 //1. 8 for(String tmp:list){ 9 System.out.println(tmp); 10 } 11 12 //2. 13 for(int i = 0;i < list.size();i++){ 14 list.get(i); 15 } 16 17 //3. 18 Iterator<String>iter = list.iterator(); 19 while(iter.hasNext()){ 20 iter.next(); 21 } 22 }
梦想要一步步来!