目录

static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

描述 (Description)

java.util.EnumSet.of(E first, E... rest)方法创建一个最初包含指定元素的枚举集。 此工厂的参数列表使用varargs功能,可用于创建最初包含任意数量元素的枚举集,但它可能比不使用varargs的重载运行得慢。

声明 (Declaration)

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

public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

参数 (Parameters)

  • first - 最初要包含的元素。

  • rest - 该集合最初要包含的其余元素。

返回值 (Return Value)

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

异常 (Exception)

NullPointerException - 如果e为null

例子 (Example)

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

/*This example is using a method called main2
to simulate calling the main method using args
from a command line.*/
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 fake list that will be used like args
      Numbers[] list = {Numbers.ONE, Numbers.THREE, Numbers.FOUR, Numbers.FIVE};
      // call the fake main
      main2(list);
   }
   // This is a fake main. This is used as an example
   public static void main2(Numbers[] fakeargs) {
      // create a set
      EnumSet<Numbers> set;
      // add first element and the rest of fakeargs
      set = EnumSet.of(Numbers.ONE, fakeargs);
      // print the set
      System.out.println("Set:" + set);
   }
}

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

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