java enumset
EnumSet类range()方法 (EnumSet Class range() method)
range() method is available in java.util package.
range()方法在java.util包中可用。
range() method is used to create an enum set and assign all the elements in the range of the given two parameters st (starting position) and en (ending position).
range()方法用于创建一个枚举集,并在给定的两个参数st(开始位置)和en(结束位置)的范围内分配所有元素。
range() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
range()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出错。
range() method may throw an exception at the time of returning enum set.
在返回枚举集时, range()方法可能会引发异常。
- NullPointerException: This exception may throw when the given any one of the parameters is null exists.NullPointerException :如果给定的任何一个参数为null,则可能引发此异常。
- IllegalArgumentException: This exception may throw when the given first parameter is greater than the second parameter.IllegalArgumentException :当给定的第一个参数大于第二个参数时,可能引发此异常。
Syntax:
句法:
public static EnumSet range(Enumset st, Enumset en);
Parameter(s):
参数:
Enumset st – represents the starting element in the enum set.
Enumset st –表示枚举集中的起始元素。
Enumset en – represents the ending element in this enum set.
Enumset en –表示此枚举集中的结束元素。
Return value:
返回值:
The return type of this method is EnumSet, it returns an enum set with elements of the given range defined.
此方法的返回类型为EnumSet ,它返回一个枚举集,其中定义了给定范围的元素。
Example:
例:
// Java program is to demonstrate the example of
// range() method of EnumSet
import java.util.*;
public class RangeOfEnumSet {
// Initialize a enum variable
// with some constants
public enum Colors {
RED,
BLUE,
GREEN,
PURPLE,
YELLOW
};
public static void main(String[] args) {
// Here , we are creating two EnumSet
EnumSet < Colors > all_of = null;
// By using allOf() method is to
// get all of the elements of an enum
// and put into an es
all_of = EnumSet.allOf(Colors.class);
// Display Modified EnumSet
System.out.println("EnumSet.allOf(Colors.class): " + all_of);
// By using range() method is to
// get all of the elements defined in the
// given range of an EnumSet
EnumSet range = EnumSet.range(Colors.GREEN, Colors.YELLOW);
// Display Modified EnumSet
System.out.println("EnumSet.range(Colors.GREEN, Colors.YELLOW): " + range);
}
}
Output
输出量
EnumSet.allOf(Colors.class): [RED, BLUE, GREEN, PURPLE, YELLOW]
EnumSet.range(Colors.GREEN, Colors.YELLOW): [GREEN, PURPLE, YELLOW]
翻译自: https://www.includehelp.com/java/enumset-range-method-with-example.aspx
java enumset