A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 芦子骐 中级黑马   /  2013-3-31 23:56  /  2389 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 芦子骐 于 2013-4-1 17:04 编辑

  今天看IO合并的时候,毕老师在用集合存储多个文件的时候,用了Enumeration这个接口。
我在百度上搜了一下他们说这个接口是1.0版本的,现在都被迭代器取代了,那这两个接口有什么不同么??
集合那快听的比较懵,大牛们如果高兴顺带着把集合的体系结构说下,那就太感激不尽了~

评分

参与人数 2技术分 +1 黑马币 +3 收起 理由
滔哥 + 1
张熙韬 + 3 赞一个!

查看全部评分

4 个回复

倒序浏览
API上是这样描述的
大概意思就是 迭代器是快速失败的,而Enumeration不是 快速失败的,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。
这是不是就是说在涉及到线程问题的时候,用Enumeration更稳定一些??

评分

参与人数 1技术分 +1 收起 理由
张熙韬 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 黄小贝 于 2013-4-1 04:10 编辑

看洒家来拯救你~~

源码注释是这样写的~~API上面的说明都是源码注释翻译过去的,很明显,英文好的可以免去API这个道具~



我自己实现了一个简易的ArrayList,里面包含了Iterator和Enumeration的用法,你可以自己调试一下
  1. import java.util.Enumeration;
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;

  4. public class MyList<T> {

  5.         private T[] theItems;
  6.         
  7.         private int size;//数组当前大小
  8.         
  9.         private static final int DEFULT_CAPACITY = 10;//数组默认初始化大小

  10.         public MyList() {
  11.                 theItems = (T[]) new Object[DEFULT_CAPACITY];
  12.         }
  13.         
  14.         public boolean add(T item){
  15.                         
  16.                 //如果最初的数组装不下了,就重新申请内存
  17.                 if(size >= DEFULT_CAPACITY){
  18.                         T[] old = theItems;
  19.                         theItems = (T[]) new Object[size * 2 + 1];
  20.                         for (int i = 0; i < old.length; i++) {
  21.                                 theItems[i] = old[i];
  22.                         }
  23.                 }

  24.                 theItems[size] = item;
  25.                 size++;
  26.                
  27.                 return true;
  28.         }
  29.         
  30.         public T remove(int index){
  31.                
  32.                 if(index < 0|| index > size ){
  33.                         throw new ArrayIndexOutOfBoundsException();
  34.                 }
  35.                
  36.                 T retVal = theItems[index];
  37.                
  38.                 for(int i = index; i < size;i++){
  39.                         theItems[i] = theItems[i+1];
  40.                 }
  41.                 theItems[size] = null;
  42.                 size--;
  43.                
  44.                 return retVal;
  45.         }
  46.         
  47.         public T get(int index){
  48.                 if(index < 0|| index > size ){
  49.                         throw new ArrayIndexOutOfBoundsException();
  50.                 }
  51.                 return theItems[index];
  52.         }
  53.         
  54.         public int size(){
  55.                 return this.size;
  56.         }

  57.         public Iterator<T> iterator() {
  58.                 return new MyIterator();
  59.         }

  60.         private class MyIterator implements Iterator<T> {
  61.                
  62.                 private int current = 0;
  63.                
  64.                 @Override
  65.                 public boolean hasNext() {
  66.                         return current < size();
  67.                 }

  68.                 @Override
  69.                 public T next() {
  70.                         if(current >= size){
  71.                                 throw new NoSuchElementException();
  72.                         }
  73.                         return theItems[current++];
  74.                 }

  75.                 @Override
  76.                 public void remove() {
  77.                         MyList.this.remove(current);
  78.                 }
  79.         }

  80.         public Enumeration<T> enumeration() {
  81.                 return new MyEnumeration();
  82.         }
  83.         
  84.         private class MyEnumeration implements Enumeration<T>{
  85.                
  86.                 private int current = 0;
  87.                
  88.                 @Override
  89.                 public boolean hasMoreElements() {
  90.                         return current < size();
  91.                 }

  92.                 @Override
  93.                 public T nextElement() {
  94.                         if(current >= size){
  95.                                 throw new NoSuchElementException();
  96.                         }
  97.                         return theItems[current++];
  98.                 }
  99.         }
  100.         
  101.         public static void main(String[] args) {
  102.                
  103.                 MyList<String> myList = new MyList<String>();
  104.                
  105.                 myList.add("a");
  106.                 myList.add("b");
  107.                 myList.add("c");
  108.                
  109.                 for (Iterator<String> iterator = myList.iterator(); iterator.hasNext();) {
  110.                         System.out.println(iterator.next());
  111.                 }
  112.                
  113.                 for(Enumeration<String> enumeration = myList.enumeration();enumeration.hasMoreElements();){
  114.                         System.out.println(enumeration.nextElement());
  115.                 }
  116.                
  117.                 for (Iterator<String> iterator = myList.iterator(); iterator.hasNext();) {
  118.                         iterator.remove();
  119.                 }
  120.                                 
  121.         }

  122. }
复制代码
讲解一下,从我的代码可以看出来,两个接口的差别很小,只是iterator多了一个remove方法







点评

施主好高端呀……谢谢了!!~虽然一眼下去看不懂,但是感觉很厉害的样子……下去慢慢研究……  发表于 2013-4-1 17:03

评分

参与人数 1技术分 +1 收起 理由
张熙韬 + 1 赞一个!

查看全部评分

回复 使用道具 举报
楼上感觉好高端~~~~~~
回复 使用道具 举报
java之所以淘汰了Enumeration是因为名称过长。采用Iterator替代了,简化了书写。Iterator比Enumerations多了一个移除方法。
上海传智学员路过。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马