Java中的Enumaration和Iterator接口

1 Enumeration

  
public interface Enumeration<E> {

    boolean hasMoreElements();

    E nextElement();
}
    An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. 
    Enumeration接口本身不是一个数据结构。但是,对其他数据结构非常重要。 Enumeration接口定义了从一个数据结构得到连续数据的手段。
    
    Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable.
    vector.class
    public Enumeration<E> elements() {
        return new Enumeration<E>() {
            int count = 0;

            public boolean hasMoreElements() {
                return count < elementCount;
            }

            public E nextElement() {
                synchronized (Vector.this) {
                    if (count < elementCount) {
                        return elementData(count++);
                    }
                }
                throw new NoSuchElementException("Vector Enumeration");
            }
        };
    }
Hashtable.class
    public synchronized Enumeration<K> keys() {
        return this.<K>getEnumeration(KEYS);
    }
    public synchronized Enumeration<V> elements() {
        return this.<V>getEnumeration(VALUES);
    }
    
    The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
    
    

2 Iterator

public interface Iterator<E> {

    boolean hasNext();

    E next();

    void remove();
}
    

    An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:

  • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  • Method names have been improved. 
     java中的集合类都提供了返回Iterator的方法,就是迭代器,它和Enumeration的主要区别其实就是Iterator可以删除元素,但是Enumration却不能。而且方法名更加合理。
    另外需要注意的是,使用Iterator来遍历集合时,应使用Iterator的remove()方法来删除集合中的元素,使用集合的remove()方法将抛出ConcurrentModificationException异常。

3 建议

      在所有输出操作中,以 Iterator 接口为最标准的输出操作,这一点始终要记住。在部分旧的操作中Enumeration 依然存在。现在普遍都使用Iterator来遍历集合类,只有特别明确声明必须使用Enumeration的才会用该类遍历集合。

      Enumeration接口是JDK1.0时推出的,是最好的迭代输出接口,最早使用Vector(现在推荐使用ArrayList)时就是使用Enumeration接口进行输出。虽然Enumeration是一个旧的类,但是在JDK1.5之后为Enumeration类进行了扩充,增加了泛型的操作应用。

   Enumeration接口常用的方法有hasMoreElements()(判断是否有下一个值)和 nextElement()(取出当前元素),这些方法的功能跟Iterator类似,只是Iterator中存在删除数据的方法,而此接口不存在删除操作。

      Enumeration和Iterator接口功能相似,而且Iterator的功能还比Enumeration多,那么为什么还要使用Enumeration?这是因为java的发展经历了很长时间,一些比较古老的系统或者类库中的方法还在使用Enumeration接口,因此为了兼容,还是需要使用Enumeration。

    这里提到了ArrayList和Vector,我们也补充一下两者的区别:

3 代码实现

package pkgOne;

import java.util.Enumeration;
import java.util.Iterator;


public class Student {
	private int[] scores=new int[100];
	private int size=0;
	public void addScore(int score) {
		scores[size++]=score;
	}
	public Enumeration<Integer> getEnumeration() {
		return new Enumeration<Integer>() {
			private int count=0;

			@Override
			public boolean hasMoreElements() {
				// TODO Auto-generated method stub
				return (count<size);
			}

			@Override
			public Integer nextElement() {
				// TODO Auto-generated method stub
				return scores[count++];
			}
		};
	}
	//真正完善的用法真参照ArrayList的iterator()方法,
	//我在这里只是简单的实现基本功能,至于Iterator基本的规则,我们先不管。
	public Iterator<Integer> getIterator() {
		return new Iterator<Integer>() {
			private int current=0;
			private int pre=-1;

			@Override
			public boolean hasNext() {
				// TODO Auto-generated method stub
				return current<size;
			}

			@Override
			public Integer next() {
				// TODO Auto-generated method stub
				pre++;
				return scores[current++];
			}

			@Override
			public void remove() {
				// TODO Auto-generated method stub
				//for循环赋值操作,更改pre和current下标
				
			}
		};
	}
}


4 参考文章



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值