比较相似术语之间的差异

之间的区别

首页 / 技术 / 它 / 编程 /枚举与迭代器的区别

枚举与迭代器的区别

2011年6月13日发布的Indika

枚举和迭代器

在Java中有许多数据结构充当集合,比如向量、哈希表和实现Java集合框架的类(即HashMap、HashSet、ArrayList、TreeSet、TreeMap、LinkedList、LinkedHashMap和LinkedHashSet)。在Java中,有许多方法可以遍历对象的各个元素。Java提供了两个接口来简化这项任务。枚举和迭代器是java中的两个接口。util包that provide functionality to enumerate through sequences or objects with a set of items. Enumerator was introduced in JDK 1.0 and Iterator which was introduced in JDK 1.2 virtually duplicates the functionality of the Enumerator (within the Collections Framework).

枚举是什么?

枚举是Java中的一个公共接口,是在JDK 1.0中引入的,它提供了枚举元素序列的能力。它在java下找到。util包。当Enumeration接口由一个对象实现时,该对象可以生成一系列元素。枚举接口有两个方法。hasMoreElements()方法将测试此枚举是否包含更多元素,而nextElement()将返回序列中的下一个元素(如果至少还有一个元素)。换句话说,通过连续调用nextElement(),程序员可以访问序列中的单个元素。例如,要使用Enumerator打印Vector v1中的所有元素,可以使用以下代码片段。

枚举e = v1.elements();

而(e.hasMoreLements ()) {

System.out.println (e.nextElement ());

}

枚举器还可以用于定义SequenceInputStream对象的输入流。

迭代器是什么?

Iterator是Java中的公共接口。util包,which allows iterating through elements of the collections objects that implement the Collections framework (such as ArrayList, LinkedList, etc.). This was introduced in JDK 1.2 and replaced the Enumerator within the Java Collections Framework. Iterator has three methods. The method hasNext() tests whether there are remaining elements in the collection and the next() method returns the next element in the series. The remove() method can be used to remove the current element from the underlying collection. For example, to print all elements in Vector v1 using Iterator, the following code snippet can be used.

迭代器i = v1.elements();

而(i.hasNext ()) {

System.out.println (e.next ());

}

枚举和迭代器的区别是什么?

尽管,枚举和迭代器是java中的两个接口。util包,which allow iterating/enumerating through elements of a series, they have their differences. Actually, Iterator, which was introduced after Enumeration, replaces the Enumeration within the Java Collections framework. Unlike Enumeration, Iterator is fail-safe. This means that concurrent modifications (to the underlying collection) are not allowed when Iterator is used. This is very useful in multi-threaded environments where there is always a risk of concurrent modifications. In the event of a concurrent modification, the Iterator object will throw a ConcurrentModificationException. Iterator has shorter method names compared to Enumerator. Furthermore, iterator has the additional functionality of deleting elements during the iteration (which is not possible using Enumerator). So, if there is a need to remove elements from the collection, Iterator is the only option that can be considered.

相关文章:

Java和J2EE的区别 Java和C语言的区别 程序设计中过程和函数的区别 Hashtable和Hashmap的区别 虚拟与抽象的区别

了下:编程标记:枚举,hasMoreElements (),hasNext (),迭代器,Java,Java集合框架,Java接口,java。util包,JDK 1.0,JDK 1.2,next (),nextElement (),Java公共接口,remove (),迭代器的三个方法

关于作者:Indika

Indika,二元同步通信。工程硕士,计算机工程博士。计算机科学副教授,主要研究方向为生物信息学、计算生物学和生物医学自然语言处理。

留下一个回复取消回复

你的电邮地址不会被公布。必填字段被标记*

请求的文章

特色的帖子

冠状病毒和感冒症状的区别

冠状病毒和感冒症状的区别

冠状病毒与SARS的区别

冠状病毒与SARS的区别

冠状病毒与流感的区别

冠状病毒与流感的区别

冠状病毒和Covid - 19的区别

冠状病毒和Covid - 19的区别

你可能会喜欢

仪式与传统的区别

可逆抑制与不可逆抑制的区别

可逆抑制与不可逆抑制的区别

EIN和TIN的区别

抗生素和抗菌剂的区别

电位差与电压之差

最新的文章

  • 酒石酸泰乐新和磷酸泰乐新有什么区别
  • 肉皮癣和大众癣有什么区别
  • 甲基橙和酚酞有什么区别
  • 黄褐斑和黄褐斑有什么区别
  • 百里香芳醇和百里香酚的区别是什么
  • MCT和LCT的区别是什么
  • 首页
  • 职位空缺
  • 关于
  • 请求的文章
  • 联系我们

版权©2010 - 2018之间的区别.保留所有权利。使用条款和隐私政策:法律.