R
Roedy Green
I am working on a an essay on EnumSet for the Java glossary.
I wrote a little test program like this and works:
-------------
package com.mindprod.practice;
public enum Breed { DALMATIAN, LABRADOR, DACHSHUND }
--------------
package com.mindprod.practice;
import static com.mindprod.practice.Breed.*;
import java.util.EnumSet;
/**
* test drive the EnumSet features
*/
public class EnumSetTest
{
/**
* main class
*
* @param args not used
*/
public static void main ( String[] args )
{
// create a set by explicitly listing the individual elements
EnumSet<Breed> breeds = EnumSet.of( DALMATIAN, DACHSHUND );
System.out.println( breeds );
}
}
-------------
When I look at the code for EnumSet.of I find:
/**
* Creates an enum set initially containing the specified
elements.
*
* Overloadings of this method exist to initialize an enum set
with
* one through five elements. A sixth overloading is provided
that
* uses the varargs feature. This overloading may be used to
create an
* an enum set initially containing an arbitrary number of
elements, but
* is likely to run slower than the overloadings that do not use
varargs.
*
* @param e1 an element that this set is to contain initially
* @param e2 another element that this set is to contain initially
* @throws NullPointerException if any parameters are null
* @return an enum set initially containing the specified elements
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
EnumSet<E> result = noneOf(e1.getDeclaringClass());
result.add(e1);
result.add(e2);
return result;
}
1. how come I don't have to put in:
EnumSet<Breed> breeds = EnumSet<Breed>.of( DALMATIAN, DACHSHUND );
I presume somehow it is inferring it from the types of the two parms.
2. Why is the compiler rejecting it when I do?
I wrote a little test program like this and works:
-------------
package com.mindprod.practice;
public enum Breed { DALMATIAN, LABRADOR, DACHSHUND }
--------------
package com.mindprod.practice;
import static com.mindprod.practice.Breed.*;
import java.util.EnumSet;
/**
* test drive the EnumSet features
*/
public class EnumSetTest
{
/**
* main class
*
* @param args not used
*/
public static void main ( String[] args )
{
// create a set by explicitly listing the individual elements
EnumSet<Breed> breeds = EnumSet.of( DALMATIAN, DACHSHUND );
System.out.println( breeds );
}
}
-------------
When I look at the code for EnumSet.of I find:
/**
* Creates an enum set initially containing the specified
elements.
*
* Overloadings of this method exist to initialize an enum set
with
* one through five elements. A sixth overloading is provided
that
* uses the varargs feature. This overloading may be used to
create an
* an enum set initially containing an arbitrary number of
elements, but
* is likely to run slower than the overloadings that do not use
varargs.
*
* @param e1 an element that this set is to contain initially
* @param e2 another element that this set is to contain initially
* @throws NullPointerException if any parameters are null
* @return an enum set initially containing the specified elements
*/
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
EnumSet<E> result = noneOf(e1.getDeclaringClass());
result.add(e1);
result.add(e2);
return result;
}
1. how come I don't have to put in:
EnumSet<Breed> breeds = EnumSet<Breed>.of( DALMATIAN, DACHSHUND );
I presume somehow it is inferring it from the types of the two parms.
2. Why is the compiler rejecting it when I do?