• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use ArrayLists in an enumeration?

 
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do also:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic