• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use ArrayLists in an enumeration?

 
John Meehan
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm working on a new project and I'm having trouble getting started. I'm trying to represent 2d objects with a 2d array of booleans (showing true if there's a pixel/block there, false if not). These objects need to be rotated by the user in 90 degree increments, so I figured I'd represent the object itself with an ArrayList of these 2d booleans, then every time the object was rotated I'd just focus on the next or previous element in the array, depending on whether the object was rotated left or right. Finally I'm trying to stick all of these in an enumeration, since I've got a limited number of separate objects that need to be represented, and this way I can (hopefully) create instances of each object in a single class instead of one for each type of object.

But, I'm running into a wall. Normally you'd have to create the ArrayList, then do Object.add() for each 2D array element of the array list, but as this is an enumeration, I don't really see how to create the ArrayLists for the enumerations. Do I assert the enumerations first, then do the ArrayList? Do I have to create the ArrayLists as I assert the enumerations? The method I'm using makes sense to me, I'm just not sure how to put it in code.

Thanks for any help.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "asserting an enumeration"?

You can create a list that holds enum instances just like you would create a list that holds any other kind of objects:

 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do also:
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Meehan wrote:Finally I'm trying to stick all of these in an enumeration, since I've got a limited number of separate objects that need to be represented, and this way I can (hopefully) create instances of each object in a single class instead of one for each type of object.

First, do you mean an 'enum' (ie, a subclass of java.lang.Enum), or an 'Enumeration' (ie, a java.util.Enumeration)? It sounds like the first.

But, I'm running into a wall. Normally you'd have to create the ArrayList, then do Object.add() for each 2D array element of the array list, but as this is an enumeration, I don't really see how to create the ArrayLists for the enumerations.

I think Jesper and Herbert have shown you a couple of ways, but I think my question would be: do you actually want a List? More specifically, will it contain duplicates?
If not, and assuming that you are talking about an enum, you might be better off with an EnumSet. It'll also likely be much faster.

Also, I'm not sure exactly how you're storing these 'pixel' booleans, but you might want to look at a BitSet as an alternative.

Winston
 
John Meehan
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did mean an enum, sorry about that.

Jesper de Jong wrote:What do you mean by "asserting an enumeration"?

You can create a list that holds enum instances just like you would create a list that holds any other kind of objects:



So in this case, the enums would be the 2d boolean arrays, then I'd create a list for each object and add the appropriate enums? Would I be able to call this from another class like normal?

I'd like to look at the BitSet later, but for now I've already started coding the rest using 2d boolean arrays.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic