📜  Java中的枚举接口

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

Java中的枚举接口

Java.util.Enumeration接口是预定义的接口之一,它的对象用于从集合框架变量(如 Stack、Vector、HashTable 等)中向前检索数据,而不是向后检索。此接口已被迭代器取代。

枚举接口定义了我们可以枚举元素集合中的元素的函数。对于新代码,枚举被认为是过时的。但是,遗留类的一些方法(例如向量和属性)、一些 API 类、应用程序代码使用此 Enumeration 接口。

重要功能

  • 枚举是同步的。
  • 它不支持添加、删除或替换元素。
  • 可以使用枚举向前访问旧集合的元素。
  • 遗留类具有处理枚举并返回枚举对象的方法。

宣言

public interface Enumeration

其中E是存储在集合中的元素类型。

Enumeration 接口的子接口是 NamingEnumeration,实现类是 StringTokenizer。

创建枚举对象

Vector ve = new Vector();
Enumeration e = v.elements();

例子:

Java
// Java program to test Enumeration
import java.util.Vector;
import java.util.Enumeration;
  
public class EnumerationClass {
  
    public static void main(String args[])
    {
        Enumeration months;
        Vector monthNames = new Vector<>();
  
        monthNames.add("January");
        monthNames.add("Febraury");
        monthNames.add("March");
        monthNames.add("April");
        monthNames.add("May");
        monthNames.add("June");
        monthNames.add("July");
        monthNames.add("August");
        monthNames.add("September");
        monthNames.add("Octobor");
        monthNames.add("November");
        monthNames.add("December");
        months = monthNames.elements();
  
        while (months.hasMoreElements()) {
            System.out.println(months.nextElement());
        }
    }
}


Java
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String args[])
        throws IOException
    {
        // creating the FileInputStream objects for all the
        // files
        FileInputStream fin
            = new FileInputStream("file1.txt");
        FileInputStream fin2
            = new FileInputStream("file2.txt");
        FileInputStream fin3
            = new FileInputStream("file3.txt");
        FileInputStream fin4
            = new FileInputStream("file4.txt");
        // creating Vector object to all the stream
        Vector v = new Vector();
        v.add(fin);
        v.add(fin2);
        v.add(fin3);
        v.add(fin4);
  
        // creating enumeration object by calling the
        // elements method
        Enumeration e = v.elements();
  
        // passing the enumeration object in the constructor
        SequenceInputStream bin
            = new SequenceInputStream(e);
        int i = 0;
        while ((i = bin.read()) != -1) {
            System.out.print((char)i);
        }
        bin.close();
        fin.close();
        fin2.close();
    }
}


Java
import java.util.Enumeration;
import java.lang.reflect.Array;
  
public class EnumerationClass implements Enumeration {
    private int size;
    private int cursor;
    private final Object array;
    public EnumerationClass(Object obj)
    {
        Class type = obj.getClass();
        if (!type.isArray()) {
            throw new IllegalArgumentException(
                "Invald type: " + type);
        }
        size = Array.getLength(obj);
        array = obj;
    }
    public boolean hasMoreElements()
    {
        return (cursor < size);
    }
    public object nextElements()
    {
        return Array.get(array, cursor++);
    }
}


Java
import java.util.*;
import java.io.*;
  
public class EnumerationExample {
    public static void main(String args[])
    {
        // String Array Creation
        String str[] = { "apple", "facebook", "google" };
  
        // Array Enumeration Creation
        ArrayEnumeration aenum = new ArrayEnumeration(str);
  
        // usageof array enumeration
        while (aenum.hasMoreElements()) {
            System.out.println(aenum.nextElement());
        }
    }
}


输出
January
Febraury
March
April
May
June
July
August
September
Octobor
November
December

带有 SequenceInputStream 的Java枚举接口

Java

import java.io.*;
import java.util.*;
class GFG {
    public static void main(String args[])
        throws IOException
    {
        // creating the FileInputStream objects for all the
        // files
        FileInputStream fin
            = new FileInputStream("file1.txt");
        FileInputStream fin2
            = new FileInputStream("file2.txt");
        FileInputStream fin3
            = new FileInputStream("file3.txt");
        FileInputStream fin4
            = new FileInputStream("file4.txt");
        // creating Vector object to all the stream
        Vector v = new Vector();
        v.add(fin);
        v.add(fin2);
        v.add(fin3);
        v.add(fin4);
  
        // creating enumeration object by calling the
        // elements method
        Enumeration e = v.elements();
  
        // passing the enumeration object in the constructor
        SequenceInputStream bin
            = new SequenceInputStream(e);
        int i = 0;
        while ((i = bin.read()) != -1) {
            System.out.print((char)i);
        }
        bin.close();
        fin.close();
        fin2.close();
    }
}

创建自定义枚举

Java

import java.util.Enumeration;
import java.lang.reflect.Array;
  
public class EnumerationClass implements Enumeration {
    private int size;
    private int cursor;
    private final Object array;
    public EnumerationClass(Object obj)
    {
        Class type = obj.getClass();
        if (!type.isArray()) {
            throw new IllegalArgumentException(
                "Invald type: " + type);
        }
        size = Array.getLength(obj);
        array = obj;
    }
    public boolean hasMoreElements()
    {
        return (cursor < size);
    }
    public object nextElements()
    {
        return Array.get(array, cursor++);
    }
}

使用字符串数组创建Java枚举

Java

import java.util.*;
import java.io.*;
  
public class EnumerationExample {
    public static void main(String args[])
    {
        // String Array Creation
        String str[] = { "apple", "facebook", "google" };
  
        // Array Enumeration Creation
        ArrayEnumeration aenum = new ArrayEnumeration(str);
  
        // usageof array enumeration
        while (aenum.hasMoreElements()) {
            System.out.println(aenum.nextElement());
        }
    }
}

枚举接口的方法

  • E –元素类型

Modifier And Type  

Method              

Explanation

default IteratorasIterator()        This method returns an Iterator which traverses all the remaining elements covered by this enumeration.
booleanhasMoreElements()                             On implementation, it returns the boolean value if there are more elements to extract or not and returns false when all the elements have been enumerated.
EnextElement()This method returns the next element of the enumeration. It throws NoSuchElementException when there are no more elements.