File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to use ArrayLists in an enumeration? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use ArrayLists in an enumeration?" Watch "How to use ArrayLists in an enumeration?" New topic
Author

How to use ArrayLists in an enumeration?

John Meehan
Greenhorn

Joined: Oct 28, 2011
Posts: 12

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

You could do also:


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
John Meehan
Greenhorn

Joined: Oct 28, 2011
Posts: 12

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use ArrayLists in an enumeration?
 
Similar Threads
Out of Sorts
How can I manually add values to ArrayList[]
Clarification of underlying code.
Arrays of strings from a Vector
outputing an arraylist of arraylists to a file.