java 枚举迭代_Java中的枚举和迭代器之间的区别

java 枚举迭代

Java中的枚举与迭代器 (Enumeration vs Iterator in Java)

Here, we will see how Enumeration differs from Iterator?

在这里,我们将看到Enumeration与Iterator有何不同?

1)枚举 (1) Enumeration)

  • Enumeration is an interface which is introduced in java.

    枚举是Java中引入的接口。

  • While iterating an element by Enumeration we can traverse only legacy elements so here we will see the concept of legacy

    在通过枚举迭代元素时,我们只能遍历旧元素,因此在这里我们将看到旧概念

    Legacy: Previous or Earlier versions of java define various classes and one interface to store objects and collection framework didn't include at the time so when collection framework came so these classes are re-engineered to support collection framework.

    旧版: Java的早期或早期版本定义了各种类,并且一个用于存储对象的接口和当时不包含收集框架的接口,因此当收集框架出现时,这些类就被重新设计以支持收集框架。

  • We can create an Enumeration object by using elements() method of Enumeration interface.

    我们可以使用Enumeration接口的elements()方法创建一个Enumeration对象。

    Syntax:

    句法:

    Enumeration en = collection_object.elements();
    
  • While iterating an object or an element by Enumeration then we can perform only read operation.

    当通过Enumeration迭代对象或元素时,我们只能执行读取操作。

  • Enumeration is faster than Iterator.

    枚举比Iterator快。

  • In the case of Enumeration, we will use two methods to check the existing element and iterating the next element.

    在枚举的情况下,我们将使用两种方法检查现有元素并迭代下一个元素。

    1. boolean hasMoreElements()
    2. Object nextElement()
  • Enumeration concept is applicable only for legacy classes so it does not support few collections that's why it is not a universal interface.

    枚举概念仅适用于遗留类,因此它不支持少量集合,这就是为什么它不是通用接口的原因。

  • By using Enumeration we can get only ReadAccess and we can't perform any remove operation.

    通过使用枚举,我们只能获取ReadAccess,而不能执行任何删除操作。

Example:

例:

import java.util.*;
class EnumerationClass {
public static void main(String[] args) {
// creating a Vector instance
Vector v = new Vector();
// add few elements by using addElement() of Vector class
v.addElement(10);
v.addElement(20);
v.addElement(30);
v.addElement(40);
v.addElement(50);
// print the vector list
System.out.println(v);
// creating an object of Enumeration 
Enumeration en = v.elements();
// loop to check existing more elements
while (en.hasMoreElements()) {
Integer in = (Integer) en.nextElement();
}
System.out.println(v);
}
}

Output

输出量

E:\Programs>javac EnumerationClass.java
Note: EnumerationClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
E:\Programs>java EnumerationClass
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50] 

2)迭代器 (2) Iterator)

  • Iterator is an interface which is introduced in java.

    Iterator是Java中引入的接口。

  • While iterating an element by Iterator we can perform read and remove operation and we can't perform any other operation like add an object, replace an object.

    在使用Iterator迭代元素时,我们可以执行读取和删除操作,而不能执行任何其他操作,例如添加对象,替换对象。

  • We can create an Iterator object by using iterator() method of Iterator interface.

    我们可以使用Iterator接口的iterator()方法创建一个Iterator对象。

    Syntax:

    句法:

    Interface_name object_name = Collection_class.Iterator_method
    
  • Iterator is slower than Enumeration.

    迭代器比枚举慢。

  • In the case of Iterator, we will use two methods to check the existing element and iterating the next element.

    就Iterator而言,我们将使用两种方法来检查现有元素并迭代下一个元素。

    1. boolean hasNext()
    2. Object next()
  • Iterator concept is not applicable only for legacy classes but also support for non-legacy classes so that's why it is a universal interface.

    迭代器概念不仅适用于遗留类,而且还支持非遗留类,因此这就是通用接口的原因。

Example:

例:

import java.util.*; 
class IteratorClass {
public static void main(String[] args) {
// Creating a Set interface object
Set set = new HashSet();
// By using add() method to add few elements in set
set.add("Java");
set.add("Python");
set.add("C++");
set.add("Ruby");
set.add("C");
// Creating an object of Iterator interface
Iterator itr = set.iterator();
// loop for traversing an element
while (itr.hasNext()) {
String str = (String) itr.next();
if (str.equals("C"))
itr.remove();
}
// Display elements of Set interface 
System.out.println("The final list of Iterator :" + set);
}
}

Output

输出量

E:\Programs>javac IteratorClass.java
Note: IteratorClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
E:\Programs>java IteratorClass
The final list of Iterator :[Ruby, Python, C++, Java]

翻译自: https://www.includehelp.com/java/differences-between-enumeration-and-iterator-in-java.aspx

java 枚举迭代

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545517.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ORA-14551: 无法在查询中执行 DML 操作

最近在调试一个带DML操作的函数时,一直不成功,在PL/SQL中测试时没问题,通过SQL语句调用函数时就不行了,刚开始一直没找到原因,后来无意间把 函数中捕获异常的代码注释掉,终于通过SQL调试时,弹出…

第五章I/O管理

I/O章节5.1.1I/O分类(1)按使用特性分(2)I/O设备按传输速率分类(3)I/O设备按信息交换的单位分5.1.2I/O控制器5.1.3I/O控制方式(1)程序直接控制方式(轮询)&…

阿里巴巴分布式服务框架 Dubbo

1.Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000 个服务提供3,000,000,000 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo自2011年开源后,已被许多非阿里系公司使用。 2.入门文档 http://alibaba.github.io/d…

列表使用与内部实现原理

列表类型 (List) 是一个使用链表结构存储的有序结构,它的元素插入会按照先后顺序存储到链表结构中,因此它的元素操作 (插入\删除) 时间复杂度为 O(1),所以相对来说速度还是比较快的,但它的查询时间复杂度为 O(n),因此查询可能会比较慢。 1 基础使用 列表类型的使用相对来…

c ++查找字符串_C ++类和对象| 查找输出程序| 套装1

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {privateint A;privateint B;publicvoid init(){A 10;B 20;}publicvoid print(){cout << A << " " << B;}};int main(){Sample S;S.init…

Oracle 练习P297 131026 PL/SQL块程序

--1、编写一个PL/SQL块&#xff0c;输出所有员工的员工姓名&#xff0c;员工号、工资和部门号。beginfor v_emp in (select * from emp) loopdbms_output.put(员工姓名&#xff1a; || v_emp.ename);dbms_output.put(&#xff0c;员工号&#xff1a; || v_emp.empno);dbms_outp…

操作系统习题

操作系统习题习题一一、选择习题二一、选择二、综合题习题三一、选择题&#xff1f;二、简答题进程互斥遵循的四个原则&#xff1a;空闲让进、忙则等待、有限等待、让权等待重点习题四一、选择&#xff1f;&#xff1f;二、综合题死锁产生的 4 个必要条件是&#xff1a; &#…

WCF trace、log

1. 打开wcf配置&#xff1a; &#xff12;. enable trace &#xff0c; log 可以改变log路径&#xff1a; &#xff13;. 用 SvcTraceViewer.exe &#xff08;直接在c盘下搜索&#xff09; 查看 &#xff14;. 如果想自定义trace&#xff1a; catch(Exception ex) { Trace.Writ…

字典使用与内部实现原理

字典类型 (Hash) 又被成为散列类型或者是哈希表类型,它是将一个键值 (key) 和一个特殊的“哈希表”关联起来,这个“哈希表”表包含两列数据:字段和值。例如我们使用字典类型来存储一篇文章的详情信息,存储结构如下图所示: 同理我们也可以使用字典类型来存储用户信息,并且…

游标复习笔记

--while循环访问游标declarecursor cur_dept isselect * from dept;v_dept cur_dept%rowtype;beginopen cur_dept;fetch cur_dept into v_dept;while cur_dept%found loopdbms_output.put_line(v_dept.dname);fetch cur_dept into v_dept;end loop;close cur_dept;end;--retur…

操作系统中同步_操作系统中的经典同步问题

操作系统中同步经典同步问题 (Classical synchronization problem) In this section, we present a number of different philosopher synchronization problems that are important mainly because they are examples for a large class of concurrency- control problems. Th…

算法设计与分析复习第一二章(时间复杂度和蛮力法)

算法复习一二章第一章时间复杂度第二章蛮力法&#xff08;1&#xff09;查找问题顺序查找&#xff08;2&#xff09;排序问题选择排序起泡排序&#xff08;3&#xff09;组合问题0-1bag问题概述&#xff08;略&#xff09;&#xff08;4&#xff09;图问题哈密顿回路TSP问题&am…

有序集合使用与内部实现原理

有序集合类型 (Sorted Set) 相比于集合类型多了一个排序属性 score(分值),对于有序集合 ZSet 来说,每个存储元素相当于有两个值组成的,一个是有序结合的元素值,一个是排序值。有序集合的存储元素值也是不能重复的,但分值是可以重复的。 当我们把学生的成绩存储在有序集…

Ubuntu12环境下Thin+rails(4)+ruby(2)+nginx+mysql 配置

Ubuntu12环境下Thinrails(4)ruby(2)nginxmysql配置1&#xff0e; 前提条件&#xff1a;已经正确安装了ubuntu12并且更行了源。2&#xff0e; 安装过程&#xff1a;2.1 安装ruby前的准备&#xff1a;1.1修改 /etc/apt/sources.list文件改为mirrors.163.com保存退出…

Oracle 游标的练习

--1、什么是游标&#xff1f;使用游标的基本步骤是什么&#xff1f; /*挡在PL/SQL块中执行查询语句&#xff08;SELECT&#xff09;和数据操纵语句&#xff08;DML&#xff09;时&#xff0c;Oracle会在内存中分配一个缓冲区&#xff0c;缓冲区中包含了处理过程的必需信息&…

集合使用与内部实现原理

集合类型 (Set) 是一个无序并唯一的键值集合。 之所以说集合类型是一个无序集合,是因为它的存储顺序不会按照插入的先后顺序进行存储,如下代码所示: 127.0.0.1:6379> sadd myset v2 v1 v3 #插入数据 v2、v1、v3 (integer) 3 127.0.0.1:6379> smembers myset #查询数…

parse 日期_日期parse()方法以及JavaScript中的示例

parse 日期JavaScript Date parse()方法 (JavaScript Date parse() method) parse() method is a Date class method, it is used to parse a given date string and returns the total number of milliseconds since 01st January 1970 (midnight) to given date string. pars…

ORA-01002 提取违反顺序

ORA-01002 提取违反顺序 ORA-01002 ORA-01002: fetch out of sequence Cause: This error means that a fetch has been attempted from a cursor which is no longer valid. Note that a PL/SQL cursor loop implicitly does fetches, and thus may also cause this error. Th…

Android 友盟SDK 终极解决报错:SocialSDK_QQZone_2.jar contains native libraries that

转自&#xff1a;http://bbs.umeng.com/thread-6552-1-2.html 报错信息&#xff1a;The library SocialSDK_QQZone_2.jar contains native libraries that will not run on the device.解决方案&#xff1a;此问题和Eclipse环境有关&#xff0c;按照如下步骤操作即可Eclipse-&g…

Redis 持久化——AOF

使用 RDB 持久化有一个风险,它可能会造成最新数据丢失的风险。因为 RDB 的持久化有一定的时间间隔,在这个时间段内如果 Redis 服务意外终止的话,就会造成最新的数据全部丢失。 可能会操作 Redis 服务意外终止的条件: 安装 Redis 的机器停止运行,蓝屏或者系统崩溃;安装 R…