如何为hashmap提供多个键

时间:2014-11-26 16:17:29

标签: hashmap

private void createRooms()
    {

myNeighbor = new HashMap <String, Room> ();

    crumbs = new Item("Crumbs", "small crumbs of some kind of food", 100);
    eggs = new Item("Raw Eggs", "a couple of raw eggs still contained within their egg shells", 1100);
    cellPhone = new Item("Cell Phone", "Mike's cell phone he must have forgotten here...", 0);
    textBooks = new Item("Textbooks", "Jay's textbooks, because he can't use his bedroom to store his stuff", 0);
    poptarts = new Item("Pop Tarts", "an un-opened box of chocolate pop tarts that someone must have left behind...", 1500);
    pizzaRolls = new Item("Pizza Rolls", "cooked steaming pizza rolls piled high", 2000);
    clothes = new Item("Clothes", "clothes, a lot of clothes all over the floor and all over the room, who knows if they're clean or not...", 0);
    //        miningTools = new Item("Mining Tools", "pickaxes, drills, and everything else you need to extract rocks and minerals from the earth's crust", 100);
    chips = new Item("Chips", "chip bag hidden away that is only half full now", 400);

    hallway = new Room("in a dark hallway with crumbs scattered over the ground", crumbs);
    kitchen = new Room("in a kitchen with raw eggs lying on the counter tops", eggs);
    bathroom = new Room("in a bathroom with a stand up shower, a washer, a drier, and Mike's cell phone left behind laying on the counter", cellPhone);
    livingRoom = new Room("in a living room with Jay's textbooks all over the room", textBooks);
    upstairsLobby = new Room("in a lobby at the top of the stairs with a box of pop tarts on the ground", poptarts);
    blakesRoom = new Room("in a dark room with towers of pizza rolls covering the desk and scattered across the bed", pizzaRolls);
    jaysRoom = new Room("in a cluttered room with clothes covering every inch of the floor and nothing hanging on the walls", clothes);
    mikesRoom = new Room("in a bed room with mining tools and a bag of chips hidden underneath a pillow on the bed", chips);

    hallway.addNeighbor("north", kitchen);
    hallway.addNeighbor("west", upstairsLobby);
    hallway.addNeighbor("east", livingRoom);    
    kitchen.addNeighbor("west", bathroom);
    kitchen.addNeighbor("south", hallway);
    bathroom.addNeighbor("east", kitchen);
    livingRoom.addNeighbor("west", hallway);    
    upstairsLobby.addNeighbor("north", jaysRoom);
    upstairsLobby.addNeighbor("west", blakesRoom);
    upstairsLobby.addNeighbor("east", mikesRoom);
    upstairsLobby.addNeighbor("south", hallway);
    blakesRoom.addNeighbor("east", upstairsLobby);
    jaysRoom.addNeighbor("south", upstairsLobby);
    mikesRoom.addNeighbor("west", upstairsLobby);

}

房间等级

import java.util.HashMap;
/**
 * Write a description of class Room here.
 * 
 * @author (Christopher  a date)
 */
public class Room
{
    private String description;
    private Item item;
    private HashMap <String, Room> myNeighbor;

    public Room (String pDescription)
    {
        description = pDescription;
        item = null;
        HashMap <String, Room> myNeighbor = new HashMap <String, Room> ();
    }

    public Room (String pDescription, Item pItem)
    {
        description = pDescription;
        item = pItem;
    }

    public String getDescription()
    {
        return description;
    }

    public Item getItem()
    {
        return item;
    }

    public void addItem(Item i)
    {
        item = i;
    }

    public boolean hasItem()
    {
        if (item != null)
            return true;
        else 
            return false;
    }

    public void addNeighbor(String pDirection, Room r)
    {
        myNeighbor = new HashMap <String, Room> ();
        myNeighbor.put(pDirection, r);   
    }

    public Room getNeighbor(String pDirection)
    {
        Room next = myNeighbor.get(pDirection);

        if(next != null){
            return next;
        }
        else{
            return null;
        }
    }

    public Item removeItem()
    {
        Item temp;
        temp = item;
        item = null;
        return temp;
    }

    public String getLongDescription()
    {
        String part1 = "You are " + description;
        String part2 = "You see ";
        if(item != null){
            return part1 + "" + part2 + "" + item.getDescription() + "" + item.getCalories();
        }
        return part1;
    }
}

长话短说,这就是添加房间并能够导航它们并拾取物品并放下它们。当我试图运行我不能有多个北/南/东/西键的程序时,它刚刚引起我的注意。我怎样才能解决这个问题,以便我可以做到这一点?

2 个答案:

答案 0 :(得分:0)

它不允许我发表评论......

我不确定你的ROOM类是什么样子但是我猜测它是在构造函数中使用hasmap进行初始化,并且是一个名为addNeighbor的方法来实际修改这个哈希映射吗?

---- ----- EDIT

查看AddNeighbor方法表明,每次向hashmap添加邻居时都会创建新的hasmap。没有必要,你在构造函数中疯狂地使用MyNeighbor,现在你可以在哈希映射中“放置”它们新的键值组合

每次都删除该行以创建新的hasmap。

答案 1 :(得分:0)

假设您希望能够写:

Room targetRoom = currentRoom.neighbour("north");

然后你需要改变你的设计。

邻居需要成为房间的成员(ivars),例如:

class Room;
typedef HashMap<string, Room*> NeighbouringRooms;

public class Room {
   ...
   public NeighbouringRooms const& neighbour() const {
     return _neighbours;
   }

private NeighbouringRooms neighbours;
}

(我在课堂上省略了一些细节,比如在房间里添加邻居。)

现在,由于只有4个可能的方向(N,S,E,W),每个房间的邻居阵列也可以做到这一点。

public class Room {
public Room neighbours[4];
  ...
}

Room room;
room.neighbour[north] = ... ;