📜  Java中的集合枚举()方法与示例

📅  最后修改于: 2022-05-13 01:55:51.869000             🧑  作者: Mango

Java中的集合枚举()方法与示例

Java.util.Collections类的enumeration()方法用于返回对指定集合的枚举。这提供了与需要枚举作为输入的旧 API 的互操作性。
句法:

public static  Enumeration enumeration(Collection c)

参数:此方法将集合c作为要为其返回枚举的参数。
返回值:此方法返回对指定集合的枚举
以下是说明enumeration()方法的示例
示例 1:

Java
// Java program to demonstrate
// enumeration() method
// for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List
            List arrlist = new ArrayList();
 
            // Adding element to srclst
            arrlist.add("Ram");
            arrlist.add("Gopal");
            arrlist.add("Verma");
 
            // Print the list
            System.out.println("List: " + arrlist);
 
            // creating object of type Enumeration
            Enumeration e = Collections.enumeration(arrlist);
 
            // Print the Enumeration
            System.out.println("\nEnumeration over list: ");
 
            // print the enumeration
            while (e.hasMoreElements())
                System.out.println("Value is: " + e.nextElement());
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// enumeration() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List
            List arrlist = new ArrayList();
 
            // Adding element to srclst
            arrlist.add(20);
            arrlist.add(30);
            arrlist.add(40);
 
            // Print the list
            System.out.println("List: " + arrlist);
 
            // creating object of type Enumeration
            Enumeration e = Collections.enumeration(arrlist);
 
            // Print the Enumeration
            System.out.println("\nEnumeration over list: ");
 
            // print the enumeration
            while (e.hasMoreElements())
                System.out.println("Value is: " + e.nextElement());
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
List: [Ram, Gopal, Verma]

Enumeration over list: 
Value is: Ram
Value is: Gopal
Value is: Verma

示例 2:

Java

// Java program to demonstrate
// enumeration() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List
            List arrlist = new ArrayList();
 
            // Adding element to srclst
            arrlist.add(20);
            arrlist.add(30);
            arrlist.add(40);
 
            // Print the list
            System.out.println("List: " + arrlist);
 
            // creating object of type Enumeration
            Enumeration e = Collections.enumeration(arrlist);
 
            // Print the Enumeration
            System.out.println("\nEnumeration over list: ");
 
            // print the enumeration
            while (e.hasMoreElements())
                System.out.println("Value is: " + e.nextElement());
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: [20, 30, 40]

Enumeration over list: 
Value is: 20
Value is: 30
Value is: 40