日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2025/3/11 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 枚举迭代_Java中的枚举和迭代器之间的区别 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

    在通過(guò)枚舉迭代元素時(shí),我們只能遍歷舊元素,因此在這里我們將看到舊概念

    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的早期或早期版本定義了各種類,并且一個(gè)用于存儲(chǔ)對(duì)象的接口和當(dāng)時(shí)不包含收集框架的接口,因此當(dāng)收集框架出現(xiàn)時(shí),這些類就被重新設(shè)計(jì)以支持收集框架。

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

    我們可以使用Enumeration接口的elements()方法創(chuàng)建一個(gè)Enumeration對(duì)象。

    Syntax:

    句法:

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

    當(dāng)通過(guò)Enumeration迭代對(duì)象或元素時(shí),我們只能執(zhí)行讀取操作。

  • 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.

    在枚舉的情況下,我們將使用兩種方法檢查現(xiàn)有元素并迭代下一個(gè)元素。

  • boolean hasMoreElements()
  • 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.

    通過(guò)使用枚舉,我們只能獲取ReadAccess,而不能執(zhí)行任何刪除操作。

Example:

例:

import java.util.*;class EnumerationClass {public static void main(String[] args) {// creating a Vector instanceVector v = new Vector();// add few elements by using addElement() of Vector classv.addElement(10);v.addElement(20);v.addElement(30);v.addElement(40);v.addElement(50);// print the vector listSystem.out.println(v);// creating an object of Enumeration Enumeration en = v.elements();// loop to check existing more elementswhile (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迭代元素時(shí),我們可以執(zhí)行讀取和刪除操作,而不能執(zhí)行任何其他操作,例如添加對(duì)象,替換對(duì)象。

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

    我們可以使用Iterator接口的iterator()方法創(chuàng)建一個(gè)Iterator對(duì)象。

    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而言,我們將使用兩種方法來(lái)檢查現(xiàn)有元素并迭代下一個(gè)元素。

  • boolean hasNext()
  • 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 objectSet set = new HashSet();// By using add() method to add few elements in setset.add("Java");set.add("Python");set.add("C++");set.add("Ruby");set.add("C");// Creating an object of Iterator interfaceIterator itr = set.iterator();// loop for traversing an elementwhile (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 枚舉迭代

總結(jié)

以上是生活随笔為你收集整理的java 枚举迭代_Java中的枚举和迭代器之间的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。