从HashMap

时间:2018-03-22 19:05:00

标签: java enums hashmap

我有以下课房间和枚举类方向,我如何编写一个从HashMap中选择随机密钥的方法退出并返回它,或者如果那个房间没有出口,则返回null

public class Room 
{
private String description;
private HashMap<Direction, Room> exits;        // stores exits of this room.
public Set<Character> chars;  // stores the characters that are in this room.

/**
 * Create a room described "description". Initially, it has
 * no exits. "description" is something like "a kitchen" or
 * "an open court yard".
 * @param description The room's description.
 * Pre-condition: description is not null.
 */
public Room(String description) 
{
    assert description != null : "Room.Room has null description";
    this.description = description;
    exits = new HashMap<Direction, Room>();
    chars = new HashSet<Character>();
    sane();
}
/**
 * Define an exit from this room.
 * @param direction The direction of the exit.
 * @param neighbor  The room to which the exit leads.
 * Pre-condition: neither direction nor neighbor are null; 
 * there is no room in given direction yet.
 */
public void setExit(Direction direction, Room neighbor) 
{
    assert direction != null : "Room.setExit gets null direction";
    assert neighbor != null : "Room.setExit gets null neighbor";
    assert getExit(direction) == null : "Room.setExit set for direction that has neighbor";
    sane();
    exits.put(direction, neighbor);
    sane();
    assert getExit(direction) == neighbor : "Room.setExit has wrong neighbor";
}

方向:

public enum Direction
{
NORTH("north"), WEST("west"), SOUTH("south"), EAST("east");

private String name;

/**
 * Constructor with parameter.
 * Pre-condition: name is not null.
 */
private Direction(String name)
{
    assert name != null : "Direction.Direction has null name";
    this.name = name;
    assert toString().equals(name) : "Direction.Direction produces wrong toString";
}

/**
 * Return the direction name.
 */
public String toString()
{
    return name;
}

}

3 个答案:

答案 0 :(得分:0)

注意: 由于您的Map Key是枚举方向,因此您应该始终使用相同的Key类型Direction获取值,否则它将返回null

示例:map.get(Direction.NORTH);

<强>解决方案

 Step 1: Set<Direction> directions=exits.keySet(); 
 Step 2: if(directions.keySet().size()==0){return null;} //check if there is no exit 
 Step 2: Object[] dirArray = directions.toArray();
 Step 3: Random random = new Random();
         int randIndex = random.nextInt(dirArray.length-1);
 Step 4: System.out.println(dirArray [randIndex ]); // Random Direction Here

如果您有任何疑问,请告诉我,我已经测试了上面的代码段

答案 1 :(得分:-1)

试试这个:

public Room pickOne(){
      Room room=this.exits.get(Direction.values()[new Random().nextInt(4)]);
      return room.getExits().size()==0?null:room;
}

将此(getter实现)添加到房间类:

public HashMap<Direction, Room> getExits(){
    return this.exits;
}
  

您需要为退出字段添加getter方法或将.getExits()替换为exits

答案 2 :(得分:-2)

类似的东西:

private Direction getRandomDirection() {
    if (exits.isEmpty()) {
        return null;
    }
    int rnd = new Random().nextInt(exits.keySet().size());
    return new ArrayList<>(exits.keySet()).get(rnd);
}