Java中的 EnumSet noneOf() 方法
Java.util.EnumSet.noneOf(类
句法:
public static > EnumSet noneOf(Class elementType)
参数:该方法接受一个元素类型的参数elementType ,并引用该枚举集的元素类型的类对象。
返回值:该方法不返回任何值。
异常:如果elementType为 Null,该方法将抛出NullPointerException 。
下面的程序说明了Java.util.EnumSet.noneOf() 方法的工作:
方案一:
// Java program to demonstrate noneof() method
import java.util.*;
// Creating an enum of GFG type
enum GFG {
Welcome,
To,
The,
World,
of,
Geeks
}
;
public class Enum_Set_Demo {
public static void main(String[] args)
{
// Creating an empty EnumSet
// Getting all elements from GFG
EnumSet e_set = EnumSet.allOf(GFG.class);
// Displaying the initial EnumSet
System.out.println("The first set is: " + e_set);
// Creating another empty set
EnumSet other_set = EnumSet.noneOf(GFG.class);
// Displaying the new set
System.out.println("The other set is: " + other_set);
}
}
输出:
The first set is: [Welcome, To, The, World, of, Geeks]
The other set is: []
方案二:
// Java program to demonstrate copyOf() method
import java.util.*;
// Creating an enum of CARS type
enum CARS {
RANGE_ROVER,
MUSTANG,
CAMARO,
AUDI,
BMW
}
;
public class Enum_Set_Demo {
public static void main(String[] args)
{
// Creating an empty EnumSet
// Getting all elements from CARS
EnumSet e_set = EnumSet.allOf(CARS.class);
// Displaying the initial EnumSet
System.out.println("The first set is: " + e_set);
// Creating another empty set
EnumSet other_set = EnumSet.noneOf(CARS.class);
// Displaying the new set
System.out.println("The other set is: " + other_set);
}
}
输出:
The first set is: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]
The other set is: []