黑马程序员技术交流社区
标题:
关于迭代器和Enumeration的区别
[打印本页]
作者:
芦子骐
时间:
2013-3-31 23:56
标题:
关于迭代器和Enumeration的区别
本帖最后由 芦子骐 于 2013-4-1 17:04 编辑
今天看IO合并的时候,毕老师在用集合存储多个文件的时候,用了Enumeration这个接口。
我在百度上搜了一下他们说这个接口是1.0版本的,现在都被迭代器取代了,那这两个接口有什么不同么??
集合那快听的比较懵,大牛们如果高兴顺带着把集合的体系结构说下,那就太感激不尽了~
作者:
芦子骐
时间:
2013-4-1 00:11
API上是这样描述的
大概意思就是 迭代器是快速失败的,而Enumeration不是 快速失败的,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。
这是不是就是说在涉及到线程问题的时候,用Enumeration更稳定一些??
作者:
黄小贝
时间:
2013-4-1 04:07
本帖最后由 黄小贝 于 2013-4-1 04:10 编辑
看洒家来拯救你~~
源码注释是这样写的~~API上面的说明都是源码注释翻译过去的,很明显,英文好的可以免去API这个道具~
1.png
(65.04 KB, 下载次数: 51)
下载附件
2013-4-1 04:05 上传
我自己实现了一个简易的ArrayList,里面包含了Iterator和Enumeration的用法,你可以自己调试一下
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyList<T> {
private T[] theItems;
private int size;//数组当前大小
private static final int DEFULT_CAPACITY = 10;//数组默认初始化大小
public MyList() {
theItems = (T[]) new Object[DEFULT_CAPACITY];
}
public boolean add(T item){
//如果最初的数组装不下了,就重新申请内存
if(size >= DEFULT_CAPACITY){
T[] old = theItems;
theItems = (T[]) new Object[size * 2 + 1];
for (int i = 0; i < old.length; i++) {
theItems[i] = old[i];
}
}
theItems[size] = item;
size++;
return true;
}
public T remove(int index){
if(index < 0|| index > size ){
throw new ArrayIndexOutOfBoundsException();
}
T retVal = theItems[index];
for(int i = index; i < size;i++){
theItems[i] = theItems[i+1];
}
theItems[size] = null;
size--;
return retVal;
}
public T get(int index){
if(index < 0|| index > size ){
throw new ArrayIndexOutOfBoundsException();
}
return theItems[index];
}
public int size(){
return this.size;
}
public Iterator<T> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<T> {
private int current = 0;
@Override
public boolean hasNext() {
return current < size();
}
@Override
public T next() {
if(current >= size){
throw new NoSuchElementException();
}
return theItems[current++];
}
@Override
public void remove() {
MyList.this.remove(current);
}
}
public Enumeration<T> enumeration() {
return new MyEnumeration();
}
private class MyEnumeration implements Enumeration<T>{
private int current = 0;
@Override
public boolean hasMoreElements() {
return current < size();
}
@Override
public T nextElement() {
if(current >= size){
throw new NoSuchElementException();
}
return theItems[current++];
}
}
public static void main(String[] args) {
MyList<String> myList = new MyList<String>();
myList.add("a");
myList.add("b");
myList.add("c");
for (Iterator<String> iterator = myList.iterator(); iterator.hasNext();) {
System.out.println(iterator.next());
}
for(Enumeration<String> enumeration = myList.enumeration();enumeration.hasMoreElements();){
System.out.println(enumeration.nextElement());
}
for (Iterator<String> iterator = myList.iterator(); iterator.hasNext();) {
iterator.remove();
}
}
}
复制代码
讲解一下,从我的代码可以看出来,两个接口的差别很小,只是iterator多了一个remove方法
2.png
(35.67 KB, 下载次数: 45)
下载附件
2013-4-1 04:06 上传
作者:
王瑞
时间:
2013-4-1 08:40
楼上感觉好高端~~~~~~
作者:
梁胜海
时间:
2013-4-1 10:55
java之所以淘汰了Enumeration是因为名称过长。采用Iterator替代了,简化了书写。Iterator比Enumerations多了一个移除方法。
上海传智学员路过。。。。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2