• Tags         
  • 2018-09-04  21:42:25        
  • 219 °C    

    Java.util.EnumSet.noneOf(Class elementType) Java中的方法用于创建elementType类型的空集。

    句法:

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

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

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

    异常:如果elementType为Null,则该方法抛出NullPointerException

    下面的程序说明了Java.util.EnumSet.noneOf()方法的工作原理

     Java.util.EnumSet.noneOf(Class elementType) 程序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: []

    Java.util.EnumSet.noneOf(Class elementType) 程序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: []

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5483.html