JAVA基础(六)Iterator、Enumeration和fail-fast

本文介绍了Java中的Iterator和Enumeration接口,强调了Iterator作为推荐选择的原因。同时,文章详细讨论了fail-fast机制,包括其概述、示例和原理,解释了在多线程环境下如何引发ConcurrentModificationException,以及ArrayList中fail-fast的实现细节。
摘要由CSDN通过智能技术生成

Enumeration

接口源码

根据以下源码和注释可以知道,此接口和iterator功能是相同的,并且在源码注解中也是推荐使用iterator代替它使用,Enumeration提供的两个函数只有读的操作,只有在Vector和Hashtale有它们的实现。

/**
 * 推荐使用iterator
 * NOTE: 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.
 *
 * @see     java.util.Iterator
 * @see     java.io.SequenceInputStream
 * @see     java.util.Enumeration#nextElement()
 * @see     java.util.Hashtable
 * @see     java.util.Hashtable#elements()
 * @see     java.util.Hashtable#keys()
 * @see     java.util.Vector
 * @see     java.util.Vector#elements()
 *
 * @author  Lee Boynton
 * @since   JDK1.0
 */
public interface Enumeration<E> {
   
	
    boolean hasMoreElements();
    
    E nextElement();
}

iterator

接口源码

从以下源码中可以知道,iterator代替了enumeration。提供了对元素的读和修改方法。

/**
 * An iterator over a collection.  {@code Iterator} takes the place of
 * {@link Enumeration} in the Java Collections Framework.  Iterators
 * differ from enumerations in two ways:
 * @param <E> the type of elements returned by this iterator
 * @see Collection
 * @see ListIterator
 * @see Iterable
 * @since 1.2
 */
public interface Iterator
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值