迭代器(Iterator)遍历 a9e5 的两种方法(for和while)
2018-04-01 20:47
316 查看
while循环遍历
Collection coll = new ArrayList(); coll.add("abc1"); coll.add("abc2"); coll.add("abc3"); coll.add("abc4"); Iterator it = coll.iterator(); while(it.hasNext()){ System.out.println(it.next()); }for循环遍历
Collection coll = new ArrayList(); coll.add("abc1"); coll.add("abc2"); coll.add("abc3"); coll.add("abc4"); for(Iterator it = coll.iterator(); it.hasNext(); ){ System.out.println(it.next()); }注意:开发中建议使用for循环实现迭代,因为while循环中,it是在while循环体外创建的,因此while循环结束后,it仍然在内存中占据着一定的空间。而使用for循环,在循环结束后,it的生命周期也会随之结束。
相关文章推荐
- Iterator迭代器的使用,对for和while对集合遍历并排
- iterator方法和for方法 遍历数据库user表结果集ResultSet
- 为什么使用迭代器iterator遍历Linkedlist要比普通for快
- 黑马程序员_两种遍历集合的方法Iterator接口和foreach循环
- 集合框架-for、foreach、Iterator三种遍历方法
- HashMap 的遍历 for:each 和EntrySet两种方法
- java数组遍历——iterator和for方法
- 遍历数组 iterator方法和for方法
- java数组遍历——iterator和for方法
- 遍历数组 iterator方法和for方法
- 遍历数组 iterator方法和for方法
- HashSet的两种遍历方式 迭代器 增强for
- java数组遍历——iterator和for方法
- java集合与数组遍历-iterator和for方法
- java数组遍历——iterator和for方法
- java数组遍历——iterator和for方法
- ES6 迭代器(Iterator)和 for.of循环使用方法学习(总结)
- ArrayList利用迭代器Iterator遍历方法实现数据(新闻)的删除、添加、显示功能例子
- 用for和do..while两种方法:键盘录入一个数 求阶乘的和
- java数组遍历——iterator和for方法