Iterator iterator():迭代器,集合的专用遍历方式
  A:Object next():获取元素,并移动到下一个位置。
    有时候会出现这样的错误: NoSuchElementException:没有这样的元素,因为你已经找到最后了。
  B:boolean hasNext():如果仍有元素可以迭代,则返回 true。

  问题1:能用while循环写这个程序,我能不能用for循环呢?
    for(Iterator it = c.iterator();it.hasNext();){
        Student s = (Student) it.next();
        System.out.println(s.getName() + "---" + s.getAge());
    }
  可以,并且for循环的效率高,因为这个循环用完后,it就被丢弃了,省内存。但是for循环语句不大明朗

  问题2:不要多次使用it.next()方法,

      因为每次使用都是访问一个对象。

迭代器的使用:

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList(); // 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("java"); // Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态 // Object obj = it.next();
// System.out.println(obj);
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了
// 判断是否有下一个元素,有就获取,没有就不搭理它 // if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// } // 最终版代码
while (it.hasNext()) {
// System.out.println(it.next());
String s = (String) it.next();
System.out.println(s);
}
}
}

用迭代器来写重新写上个遍历

 package zl_ObjectTest1;

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; /*
用集合存储5个动物对象,并把动物对象进行遍历,用迭代器遍历 */
public class AnimalDemo { public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList(); //创建动物对象 Animal a1 = new Animal("猫", "虎纹",2);
Animal a2 = new Animal("荷兰猪", "粉色",1);
Animal a3 = new Animal("老鹰", "黑白",4);
Animal a4 = new Animal("鹦鹉", "五颜六色",2);
Animal a5 = new Animal("警犬", "黑色", 3); //把对象放进去集合中
c.add(a1);
c.add(a2);
c.add(a3);
c.add(a4);
c.add(a5); // Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator();
while (it.hasNext()){
//System.out.println(it.next()); //向下转型
Animal a = (Animal)it.next();
System.out.println(a.getName()+"\t"+a.getColor()+"\t"+a.getAge()); // NoSuchElementException 不要多次使用it.next()方法
// System.out.println(((Student) it.next()).getName() + "---"
// + ((Student) it.next()).getAge());
// 错误的用法
//((Student) it.next()).getName()拿的是第一个的名字,
//((Student) it.next()).getAge()拿的是第二个的年龄...
} //改为for循环方法来遍历:
//for(Iterator it = c.iterator();it.hasNext();){
//Animal a = (Animal)it.next();
//System.out.println(a.getName()+"\t"+a.getColor()+"\t"+a.getAge());
//}
} }

最新文章

  1. python学习笔记-(十一)面向对象进阶&异常处理
  2. CSS基本知识0-命名规范
  3. POJ 1185 炮兵阵地(状态压缩DP)
  4. js之正则表达式(上)
  5. Computer skills one can learn within one day
  6. Linux内核与根文件系统的关系
  7. A Game of Thrones(13) - Tyrion
  8. 使用Ambari快速部署Hadoop大数据环境
  9. photoshop如何快速切图
  10. Jackson注解学习参考(转)
  11. [2014-02-19]ConfigurationSection:让web.config配置更有条理
  12. Java基础 -- Java 抽象类 抽象方法
  13. 20175226 2018-2019-2《java程序设计》结对编程-四则运算(第一周-阶段总结)
  14. python3 进行字符串、日期、时间、时间戳相关转换
  15. jsp的九大内置对象及作用
  16. SDN期末作业博客
  17. ASP.NET MVC:some benefits of asp.net mvc
  18. PendingIntent传递数据注意参数RequestCode和Flag
  19. 原创:《Excel在零售及电商行业数据化管理中的应用》之“什么是数据化管理?
  20. 132. Palindrome Partitioning II (String; DP)

热门文章

  1. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance
  2. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q54-Q56)
  3. 【读书笔记】iOS-本地文件和数据安全注意事项
  4. 朝花夕拾-android 从手机选择图片或拍照设置头像
  5. android 使用HttpURLConnection方式提交get/post请求
  6. C语言-10-位域与共用体
  7. 【AdaBoost算法】积分图代码实现
  8. 监听spring加载完成后事件
  9. apache 开启zgip 压缩模式
  10. 迷宫问题求解之“A*搜索”(二)