遍历数组、集合的三种方式

int [] array = new int[]{2,3,4,5,6,8};
        
//普通for循环
for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
}
        
//增强for循环
for (int i : array) {
    System.out.println(i);
}

//使用迭代器
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    Integer i = iterator.next();
    System.out.println(i);
}
        
                

 

posted @ 2019-07-21 16:16  Java精进之路  阅读(395)  评论(0编辑  收藏  举报