
hasNext():判断是否还有下一个元素
while(iterator。hasNext()){
//next():①指针下移 ②将下移后的集合位置上的元素返回
sout(iterator.next());
}
集合元素的遍历操作,使用迭代器Iterator接口
内部的方法:hasNext()和next();
集合对象每次调用iterator()方法都得到一个全新的迭代器对象
默认的游标都在集合的第一个元素之前
hasNext()判断是否还有下一个元素
while(iterator.hasNext){//next():①指针下移 ②将下移以后集合位置上的元素返回System.out.println(iterator.next());}

jdk 5.0 新增了foreach循环,用于遍历集合,数组
public class ForTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("jerry",20));coll.add(new String("Tom"));coll.add(false);//for (集合中元素的类型 局部变量 :集合对象)//内部仍然调用了迭代器for (Object obj : coll){System.out.println(obj);}}@Testpublic void test2(){int[] arr = new int[]{1,2,3,4,5,6};//for(数组元素的类型 局部变量 :数组对象)for(int i : arr){System.out.println(i);}}//练习题@Testpublic void test3(){String[] arr = new String[]{"MM","MM","MM"};//方式一:普通for赋值// for (int i = 0; i < arr.length; i++) {// arr[i] = "GG";// }//增强for循环for(String s : arr){s = "GG";}for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}}}
