黑马程序员技术交流社区

标题: 迭代器问题 [打印本页]

作者: 何苦似梦离    时间: 2014-2-17 10:41
标题: 迭代器问题
本帖最后由 何苦似梦离 于 2014-2-17 11:23 编辑
  1. import java.util.Collection;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;

  5. class test
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 Collection<Integer> collection = new LinkedList<Integer>();

  10.                 collection.add(4);
  11.                 collection.add(1);
  12.                 collection.add(7);
  13.                 collection.add(5);

  14.                 Iterator<Integer> it = collection.iterator();
  15.                
  16.                 for(Integer t : it) //为什么编译提示这里不用使用高级for
  17.                 {
  18.                         System.out.println(t);
  19.                 }

  20.                 System.out.println(collection);
  21.         }
  22. }
复制代码



作者: 唐志海    时间: 2014-2-17 11:09
你要循环输出的是集合,而不是迭代器。。迭代器也只是取出集合中元素的一种方式,并不是集合。所以你的for里面的it应该改为集合collection 。
  1. class forDemo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Collection<Integer> collection = new LinkedList<Integer>();

  6.                 collection.add(4);
  7.                 collection.add(1);
  8.                 collection.add(7);
  9.                 collection.add(5);

  10.                 Iterator<Integer> it = collection.iterator();
  11.                
  12.                 for(Integer t : collection ) //为什么编译提示这里不用使用高级for
  13.                 {
  14.                         System.out.println(t);
  15.                 }

  16.                 System.out.println(collection);
  17.         }
  18. }
复制代码




作者: 小Zeor………    时间: 2014-2-17 11:27
同学,谢谢你的提问,在探寻的过程中我对自己的知识体系也有了弥补。。
  1. class test
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Collection<Integer> collection = new LinkedList<Integer>();

  6.                 collection.add(4);
  7.                 collection.add(1);
  8.                 collection.add(7);
  9.                 collection.add(5);

  10.                 //Iterator<Integer> it = collection.iterator();
  11.                
  12.                 for(int t : collection) //为什么编译提示这里不用使用高级for
  13.                 {
  14.                         System.out.println(t);
  15.                 }

  16.                 System.out.println(collection);
  17.         }
  18. }
复制代码

就如:我修改的那样,你看下。
我觉得迭代器就是一个取出集合数据的方式,而高级for循环针对的是你要遍历的对象,你可以遍历一个集合,但不能遍历一个迭代器吧,集合里面存在的是每个元素,而迭代器中存储的各种方法比如,next(),itHasNext(),这样的东西。 所以不能针对迭代器遍历
作者: ghhryr    时间: 2014-2-19 22:43
学习了,开始的时候我也进入了误区,IDE提示是:Can only iterate over an array or an instance of java.lang.Iterable,翻译:iterator遍历一个为数组或是实现Iterable的子类,而上面for循环遍历的是迭代器
作者: .......    时间: 2014-2-19 22:53
是你用的方式不对。 高级for遍历的是对象不是迭代器
作者: 不再去想    时间: 2014-2-20 10:13
通过这个问题,我也了解到了以前不知道的只知识点,顶一个……
作者: 白_蓝(八公)    时间: 2014-2-20 11:20
哥们!你用错了啦!~你怎么吧迭代器的it传入到高级for循环中去了!!你直接把collection这个对象引用调用到高级for中去就可以啦!!希望对你有帮助哈
作者: 薛旻    时间: 2014-2-20 11:38
高级for循环本身就可以迭代获取集合了,也就用不着迭代器Iterator了。
如果使用了迭代器,那么也就没必要用高级for循环了,两者功能有点点冲突。
作者: 超级赛亚人    时间: 2014-3-11 22:32

通过这个问题,我也了解到了以前不知道的只知识点,顶一个……




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2