public
static <E extends Enum<E>> EnumSet<E> of(E e1, E
e2, E e3, E e4)
Creates
an enum set initially containing the specified elements.
import java.util.*; class EnumSetOf4{ enum Day{ MON,TUE,WED,THU, FRI,SAT,SUN; } public static void main(String args[]){ EnumSet<Day> mySet1; mySet1 = EnumSet.of(Day.SUN,Day.MON,Day.TUE, Day.WED); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); } }
Output
Elements in mySet1 are [MON, TUE, WED, SUN]
1. throws
NullPointerException if any parameters are null
import java.util.*; class EnumSetOf4Null{ enum Day{ MON,TUE,WED,THU, FRI,SAT,SUN; } public static void main(String args[]){ EnumSet<Day> mySet1; mySet1 = EnumSet.of(Day.SUN,Day.MON,Day.TUE,null); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.util.EnumSet.typeCheck(EnumSet.java:378) at java.util.RegularEnumSet.add(RegularEnumSet.java:160) at java.util.RegularEnumSet.add(RegularEnumSet.java:36) at java.util.EnumSet.of(EnumSet.java:274) at EnumSetOf4Null.main(EnumSetOf4Null.java:11)
No comments:
Post a Comment