Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default How can I implement an ArrayList in an enum?

    I'm making a game, and I'm trying to create a way for the character to move from room to room. My room objects are enums, and I want them to hold an ArrayList of Door objects (since there may be a different number of doors in each room), and let the player activate() the door which will transport them to the room object specified by the door.

    Heres some of my code:

    Java Code:
    public enum Room {
    	/*
    	 * Defines all rooms in the game
    	 */
    
    	TestRoom1("Test Room", Engine.printFileText("text_files/test_room_description.txt"), 101),
    	TestRoom2("Test Hall", Engine.printFileText("text_files/test_hall_description.txt"), 100);
    	
    	public int ID;
    	public String name;
    	public String description;
    	ArrayList<Door> exits;
    	
    	Room(String _name, String _description, int _ID) {
    		exits = new ArrayList<Door>();
    		ID = _ID;
    		name = _name;
    		description = _description;
    	}
    
    // Rest of class ommited
    I have no idea how to define each rooms exits and then add the doors to its specific ArrayList so that it can be constructed.

    Thanks!

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Why enums? What design you want to accomplish with this?

    Here is some example just to give you idea in which direction you could go:

    Object-Oriented Software Development Using Java | Source Code

    hope that can help, but that is just beginning !

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

    Default

    I don't think that Room should be an enum but rather a class; it just seems to me that Room is not an inert constant but rather a entity that may change, may be subclassed, and may need to have multiple instances. Room properties may be best coded as enums (may), but to not Room itself. That's just my view, and others might see it differently.

Similar Threads

  1. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. Enum example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:34 PM
  4. How to use enum
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:34 PM
  5. Enum?
    By vgbhagavan in forum Advanced Java
    Replies: 0
    Last Post: 06-14-2007, 02:02 PM

Posting Permissions

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