Java中的EnumSet noneOf()方法

爪哇。util。枚举集。不属于( 元素类型 )方法用于创建类型的空集 元素类型 .

null

语法:

public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)

参数: 该方法接受一个参数 元素类型 元素类型的,并引用此枚举集的元素类型的类对象。

返回值: 该方法不返回任何值。

例外情况: 方法抛出 空指针异常 如果 元素类型 是空的。

下面的程序说明了Java的工作原理。util。枚举集。noneOf()方法: 项目1:

// 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<GFG> e_set = EnumSet.allOf(GFG. class );
// Displaying the initial EnumSet
System.out.println( "The first set is: " + e_set);
// Creating another empty set
EnumSet<GFG> 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: []

项目2:

// 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<CARS> e_set = EnumSet.allOf(CARS. class );
// Displaying the initial EnumSet
System.out.println( "The first set is: " + e_set);
// Creating another empty set
EnumSet<CARS> 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: []

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享