免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2565 | 回复: 5

java.util下面怎么找不到CollectionIterator类型? [复制链接]

论坛徽章:
0
发表于 2012-08-01 09:05 |显示全部楼层
10可用积分
本帖最后由 donet8 于 2012-08-01 09:26 编辑

我在CentOS6.2 linux上面用eclipse编程,发现敲入java.util以后,提示列表里面的类型并没有包括CollectionIterator/SetIterator/MapIterator
但是ListIterator是有的啊。

论坛徽章:
0
发表于 2012-08-01 10:12 |显示全部楼层
因为java标准库里本来就没有这几个类!

论坛徽章:
0
发表于 2012-08-01 10:42 |显示全部楼层
_Rayx 发表于 2012-08-01 10:12
因为java标准库里本来就没有这几个类!


那么,如果我要遍历打印Set/Map/Collection中的元素,该怎么做呢?

谢谢。

论坛徽章:
0
发表于 2012-08-01 11:11 |显示全部楼层
回复 3# donet8


    实现了Iterable<E> 接口的,通过它来遍历。

论坛徽章:
0
发表于 2012-08-01 20:18 |显示全部楼层
也许《Think in Java》里的有关容器的内容会对问题的解决有所帮助,下面的代码来自《Think in Java》,个人在Eclipse、ubuntu上运行通过。

------------遍历打印-------------------
  1. /**
  2. * File:MapDataTest.java
  3. * ----------------------
  4. * This sample is a code piece in <Think in Java> "Containers in Depth"
  5. * It can run on Eclipse4.2,ubuntu12.04,OpenJdk7, you can download attachment,it is a Eclipse project.
  6. * And import "Existing Projects into Workspace", so you can test it.
  7. * Thinking in Java 4th Edition Source Code url:http://www.mindviewinc.com/TIJ4/CodeInstructions.html
  8. * OpenJDK™-7 Source code url:http://download.java.net/openjdk/jdk7/
  9. *
  10. */

  11. package containers;


  12. import java.util.*;
  13. import net.mindview.util.*;
  14. import static net.mindview.util.Print.*;

  15. class Letters implements Generator<Pair<Integer,String>>,
  16.   Iterable<Integer> {
  17.   private int size = 9;
  18.   private int number = 1;
  19.   private char letter = 'A';
  20.   public Pair<Integer,String> next() {
  21.     return new Pair<Integer,String>(
  22.       number++, "" + letter++);
  23.   }
  24.   public Iterator<Integer> iterator() {
  25.     return new Iterator<Integer>() {
  26.       public Integer next() { return number++; }
  27.       public boolean hasNext() { return number < size; }
  28.       public void remove() {
  29.         throw new UnsupportedOperationException();
  30.       }
  31.     };
  32.   }
  33. }

  34. public class MapDataTest {
  35.   public static void main(String[] args) {
  36.     // Pair Generator:
  37.     print(MapData.map(new Letters(), 11));
  38.     // Two separate generators:
  39.     print(MapData.map(new CountingGenerator.Character(),
  40.       new RandomGenerator.String(3), 8));
  41.     // A key Generator and a single value:
  42.     print(MapData.map(new CountingGenerator.Character(),
  43.       "Value", 6));
  44.     // An Iterable and a value Generator:
  45.     print(MapData.map(new Letters(),
  46.       new RandomGenerator.String(3)));
  47.     // An Iterable and a single value:
  48.     print(MapData.map(new Letters(), "Pop"));
  49.   }
  50. } /* Output:
  51. {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K}
  52. {a=YNz, b=brn, c=yGc, d=FOW, e=ZnT, f=cQr, g=Gse, h=GZM}
  53. {a=Value, b=Value, c=Value, d=Value, e=Value, f=Value}
  54. {1=mJM, 2=RoE, 3=suE, 4=cUO, 5=neO, 6=EdL, 7=smw, 8=HLG}
  55. {1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop}
  56. *///:~
复制代码
MapDataTest.tar.gz (14.64 KB, 下载次数: 8)

论坛徽章:
0
发表于 2012-08-02 13:58 |显示全部楼层
实现了Iterable<E> 接口的,通过它来遍历。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP