Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to create an array of EnumSet

  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default How to create an array of EnumSet

    I want to create an array of enums where each element of the array can can be a combination of enum constants (i.e. Bit Masks | Cocoanetics)

    I tried this:
    Java Code:
        EnumSet<BlockCharacteristic> BlockCharSet = of(WALL, STARTING_POSITION);
    
        private BlockCharSet[][] blockCharacteristic = new BlockCharSet[gridSize][gridSize];
    But I get an a "can not find class BlockCharSet" error.

    Is there another way to do it?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create an array of EnumSet

    Line #1 is syntactically incorrect: the of( ... ) method is a static method of the EnumSet class, i.e. EnumSet.of( ... ).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to create an array of EnumSet

    Ok.

    But I get a "can not find class BlockCharSet" error on line 3.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to create an array of EnumSet

    BlockCharSet is a variable name, not a type. So it should come as no surprise that you see this error.

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to create an array of EnumSet

    Ok.

    But when I change line 3 to this:

    Java Code:
    private EnumSet<BlockCharacteristic>[][] blockCharacteristic = new EnumSet<BlockCharacteristic>[gridSize][gridSize];
    I get a generic array creation error.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to create an array of EnumSet

    I think that this is not allowed due to how Java handles generics. You may need to create a container class and then an array of these.

    Edit: yes, I'm correct. It's due to Java generics not being covariant. Please have a look here: Java theory and practice: Generics gotchas
    Last edited by Fubarable; 05-19-2012 at 06:40 PM.

  7. #7
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to create an array of EnumSet

    I haven't created a container class before. Can you please point me in the right direction, i.e. a web page that does a good job of describing it)

    Thanks.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by shall View Post
    I haven't created a container class before. Can you please point me in the right direction, i.e. a web page that does a good job of describing it)
    Sorry, poor choice of words. I meant to create a *wrapper* class, just a class to wrap around your enum map.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by Fubarable View Post
    Sorry, poor choice of words. I meant to create a *wrapper* class, just a class to wrap around your enum map.
    That's not really needed, if you're willing to offer a bit of type safety; look here:

    Java Code:
    import java.util.EnumSet;
    
    public class T {
    	enum S { A, B, C };
    	
    	static EnumSet<S> set= EnumSet.of(S.A, S.B);
    	
    	public static void main(String[] args) {
    
    		EnumSet<S>[] array= new EnumSet[42];
    	}
    }
    Note how the array is just a 'raw' type of EnumSet elements; just let the compiler warn its head off; that set is just a bunch of bits anyway ;-)

    kind regards,

    Jos
    shall likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by JosAH View Post
    That's not really needed, if you're willing to offer a bit of type safety;...
    But please know that a puppy dies every time you do this.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by Fubarable View Post
    But please know that a puppy dies every time you do this.
    Who? me? I'd never do that; you know me ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by JosAH View Post
    That's not really needed, if you're willing to offer a bit of type safety; look here:

    Java Code:
    import java.util.EnumSet;
    
    public class T {
    	enum S { A, B, C };
    	
    	static EnumSet<S> set= EnumSet.of(S.A, S.B);
    	
    	public static void main(String[] args) {
    
    		EnumSet<S>[] array= new EnumSet[42];
    	}
    }
    Note how the array is just a 'raw' type of EnumSet elements; just let the compiler warn its head off; that set is just a bunch of bits anyway ;-)

    kind regards,

    Jos
    Great! I don't why the other way doesn't work. Aren't they the same? Won't the new EnumSet use S? My compiler doens't give any warnings.

    My purpose is to create a 2 dimensional array where each element can possible be a combination of one or more enum constants.

    Here's an example:

    Java Code:
    import java.util.EnumSet;
    
    public class T12 {
    
        enum S {
    
            A, B, C
        };
        
        static EnumSet<S> setA = EnumSet.of(S.A);
        static EnumSet<S> setAB = EnumSet.of(S.A, S.B);
        static EnumSet<S> setBC = EnumSet.of(S.B, S.C);
        private int x_gridSize = 20;
        private int y_gridSize = 20;
        private EnumSet<S>[][] array;
    
        public T12() {
            array = new EnumSet[x_gridSize][y_gridSize];
            
            for (int i = 0; i< x_gridSize ; i++) {
                for (int k = 0; k < y_gridSize; k++) {
                    array[i][i] = setA;
                }
            }
        }
        
        public void change()
        {
            array[10][5] = setAB;
        }
        
        public static void main(String[] args) {
            T12 t12 = new T12();
            
            t12.change();
    
        }
    }
    Actually, I want the array element to be either S.A or S.C and then add an extra S.B characteristic to it along the way.

    Is there a better or another way?
    Last edited by shall; 05-19-2012 at 10:07 PM.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create an array of EnumSet

    Quote Originally Posted by shall View Post
    Great! I don't why the other way doesn't work. Aren't they the same?
    Is there a better or another way?
    Arrays can't exist with type parameters; see the Class class for starters and pay attention to the methods and other auxiliary types that started from version 1.5. Arrays are always 'raw' types. You can declare its type as being generified, but that actual array (the 'new' operator) is always just a 'raw' type. So the compiler can always just check the 'raw' types of its elements. It's a 'deficiency' (mind the quotes) of the generic mechanism because it's supposed to be a compiler time only thingy (to prevent automatic code generation and code bloat).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: How to create an array of EnumSet

    I'm still a little lost on this one and will have to do some more research. Thanks for the help - you got me over the hump.

Similar Threads

  1. Create an array of polygons
    By jtech55 in forum Java 2D
    Replies: 0
    Last Post: 03-18-2011, 07:49 PM
  2. Create a Get/set for an array
    By mechdesignron in forum New To Java
    Replies: 4
    Last Post: 05-04-2010, 09:39 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. how create array graphically
    By sunolinu in forum AWT / Swing
    Replies: 3
    Last Post: 02-12-2010, 07:34 AM
  5. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •