• Tags
  •         
  • www.breakyizhan.com
  •    

    Java中的Java.util.EnumSet.clone()方法用于返回现有或此集的浅表副本。

    句法:

    Enum_Set_2 = Enum_Set_1.clone()

    参数:该方法不接受任何参数。

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

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

    程序1:

    // Java program to demonstrate clone() 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 empty EnumSet
            System.out.println("Initial set: " + e_set);
     
            // Cloning the set
            EnumSet<GFG> final_set = e_set.clone();
     
            // Displaying the final set
            System.out.println("The updated set is:" + final_set); 
        }
    }
    

    输出:

    Initial set: [Welcome, To, The, World, of, Geeks]
    The updated set is:[Welcome, To, The, World, of, Geeks]

    程序2:

    // Java program to demonstrate clone() 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 empty EnumSet
            System.out.println("Initial set: " + e_set);
     
            // Cloning the set
            EnumSet<CARS> final_set = e_set.clone();
     
            // Displaying the final set
            System.out.println("The updated set is:" + final_set); 
        }
    }
    

    输出:

    Initial set: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]
    The updated set is:[RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]

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