- 爪哇。util。枚举集。复印件( 收集 ) 方法用于将集合中的所有内容复制到新的枚举集。首先,集合由枚举的元素组成,然后创建一个新的枚举集,即集合的副本。
语法:
New_Enum_Set = EnumSet.copyOf(Collection collect)
参数: 该方法接受一个参数 收集 枚举的对象类型,并指其值将复制到新的_enum_集中的集合。
返回值: 该方法不返回任何值。
例外情况:
- 非法数据异常 :如果 收集 不是枚举集实例,并且包含无法与枚举进行比较的元素或不包含任何元素。
- 空指针异常 :如果 收集 是空的。
下面的程序演示了java的工作原理。util。枚举集。copyOf()方法:
// Java program to demonstrate copyOf() 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 collection
Collection<GFG> collect =
new
ArrayList<GFG>();
// Adding elements to the Collection
collect.add(GFG.Welcome);
collect.add(GFG.World);
collect.add(GFG.Geeks);
// Displaying the collection
System.out.println(
"The collection is: "
+ collect);
EnumSet<GFG> e_set = EnumSet.copyOf(collect);
// Displaying the final set
System.out.println(
"The enum set is:"
+ e_set);
}
}
输出:The collection is: [Welcome, World, Geeks] The enum set is:[Welcome, World, Geeks]
- 这个 JAVAutil。枚举集。复印件( 枚举集e_集 ) 方法用于将现有枚举集(即e_集)中的所有内容复制到新的枚举集。
语法:
New_Enum_Set = EnumSet.copyOf(EnumSet e_set)
参数: 该方法接受一个参数 e_集 枚举的对象类型,并指其值将被复制到新的_enum_集中的集合。
返回值: 该方法不返回任何值。
例外情况: 方法抛出 空指针异常 什么时候 e_集 是空的。
下面的程序演示了java的工作原理。util。枚举集。copyOf()方法:
// 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(
"Initial set is: "
+ e_set);
// Copying the set
EnumSet<CARS> new_set = EnumSet.copyOf(e_set);
// Displaying the final set
System.out.println(
"The new set is: "
+ new_set);
}
}
输出:Initial set is: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW] The new set is: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END