目录

static <E extends Enum<E>> EnumSet<E> of(E e)

描述 (Description)

java.util.EnumSet.of(E e)方法创建一个最初包含指定元素的枚举集。

声明 (Declaration)

以下是java.util.EnumSet.of()方法的声明

public static <E extends Enum<E>> EnumSet<E> of(E e)

参数 (Parameters)

e - 该集合最初要包含的元素。

返回值 (Return Value)

此方法返回最初包含指定元素的枚举集。

异常 (Exception)

NullPointerException - 如果e为null

例子 (Example)

以下示例显示了java.util.EnumSet.Of()方法的用法。

package com.iowiki;
import java.util.*;
public class EnumSetDemo {
   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };
   public static void main(String[] args) {
      // create a set
      EnumSet<Numbers> set;
      // add one element
      set = EnumSet.of(Numbers.FIVE);
      // print the set
      System.out.println("Set:" + set);
      // add another element which replaces the previous
      set = EnumSet.of(Numbers.THREE);
      // print the set. Notice that it has one element
      System.out.println("Set:" + set);
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Set:[FIVE]
Set:[THREE]
↑回到顶部↑
WIKI教程 @2018