Java教程

Java Collections enumeration()

enumeration()是Java Collections 类的一种方法,用于获取指定集合上的枚举。

语法

以下是 enumeration()方法的声明:
public static <T> Eenumeration<T> enumeration(Collection<T> c)

参数

参数 说明 必需/可选
c 这是要返回其枚举的集合。 必需

返回

enumeration()方法返回指定集合上的枚举。

异常

NA

兼容版本

Java 1.5及更高版本

示例1

import java.util.*;
public class CollectionsEnumerationExample1 {
    public static void main(String[] args) {      
        Vector<String> Enum = new Vector<String>();       
            Enum.add("JAVA");
            Enum.add("JSP");
            Enum.add("SERVLET");
            Enum.add("C");
            Enum.add("PHP");
            Enum.add("PERL");
            //Create Enumeration
            Enumeration<String> en = Collections.enumeration(Enum);
            System.out.println("The Enumeration List are: ");
            while(en.hasMoreElements()){
                 System.out.println(en.nextElement());
            }        
    }     
}
输出:
The Enumeration List are: 
JAVA
JSP
SERVLET
C
PHP
PERL

示例2

import java.util.*;
public class CollectionsEnumerationExample2 {
    public static void main(String[] args) {
        //Create array list object       
          List<Integer> Enum = new ArrayList<Integer>();             
            Enum.add(1100);
            Enum.add(2100);
            Enum.add(5100);
            //Create Enumeration
            Enumeration<Integer> en = Collections.enumeration(Enum);
            System.out.println("The Enumeration List are: ");
            while(en.hasMoreElements()){
                 System.out.println(en.nextElement());
            }        
    }     
}
输出:
The Enumeration List are: 
1100
2100
5100

示例3

import java.util.*;
public class CollectionsEnumerationExample3 {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ArrayList firstList = new ArrayList();
        firstList.add(101);
        firstList.add("Hello");
        firstList.add("World");
        firstList.add(true);
        firstList.add(101.5);  
        Enumeration<String> e = Collections.enumeration(firstList);  
        System.out.print("Elements using Enumeration: ");
        while(e.hasMoreElements()) {
              Object obj = e.nextElement();
              System.out.print(obj + " ");
              }
        }     
}   
输出:
Elements using Enumeration: 101 Hello World true 101.5 

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4