【问题标题】:How to decrement a value in a HashMap?如何减少HashMap中的值?
【发布时间】:2015-03-31 23:29:49
【问题描述】:

所以我有一个playerIDnumwalls 用于我正在制作的棋盘游戏中的每个玩家。 现在要在每个玩家都用墙的时候拆墙,基本上每个人都在共享墙。

所以我想我应该创建一个hashmap 来保存playerID 作为键和numwalls 作为值。

但是当它应该使用墙壁时,我不知道如何减少键值。

我将显示一个有问题的代码。

public int getWallsRemaining(int i) {
    return numWalls;
}

public void lastMove(PlayerMove playerMove) {
    System.out.println("in lastMove... " + playerMove);
    /**
     * if piece moves, update its position
     */
    if(playerMove.isMove() == true){

        Integer player = playerMove.getPlayerId();

        Coordinate newLoc = new Coordinate(playerMove.getEndRow(), playerMove.getEndCol());
        playerHomes.put(player, newLoc);

    }
    /**
     * if a wall is placed, subtract the wall form the player who placed it
     * and subtract the appropriate neighbors.
     */
    if(playerMove.isMove() == false){
        numWalls-=1;
        removeNeighbor(playerMove.getStart(), playerMove.getEnd());

    }


}

这是我初始化所有内容的地方,walls 是我正在尝试做的地图:

private Map<Coordinate, HashSet<Coordinate>> graph;

private int PlayerID;
private int numWalls;
private Map<Integer, Coordinate> playerHomes;
private Map<Integer, Integer> walls;



@Override
public void init(Logger logger, int playerID, int numWalls, Map<Integer, Coordinate> playerHomes) {


    this.PlayerID = playerID;
    this.walls = new HashMap<Integer, Integer>();
    this.numWalls = numWalls;
    this.playerHomes = playerHomes;
    this.graph = new HashMap<Coordinate, HashSet<Coordinate>>();
    walls.put(playerID,numWalls);

    for(int r = 0; r <= 10; r++){
        for(int c = 0; c <= 10; c++){
            HashSet<Coordinate> neighbors = new HashSet<Coordinate>();
                 if(r > 0){
                    neighbors.add(new Coordinate(r - 1, c));
                 }
                if(r < 8){
                     neighbors.add(new Coordinate(r + 1, c));
                 }
                if(c > 0){
                    neighbors.add(new Coordinate(r, c - 1));
                 }
                if(c < 8){
                    neighbors.add(new Coordinate(r, c + 1));
                 }
            graph.put((new Coordinate(r,c)), neighbors);
        }
    }
}

您可以在我的 lastMove 方法中看到我将墙减 1。这是我的问题。我想将指定的 playerID numwall 减 1。我现在只适用于 1 人。我需要它来为最多 4 名玩家工作。

【问题讨论】:

    标签: java hashmap decrement


    【解决方案1】:

    HashMap 只能包含对象(不是基元),因此您必须插入一个 Integer 作为映射值。

    由于Integer 是一个不可变类,您不能直接修改值,您需要通过丢弃旧值来替换它,例如:

    HashMap<Player, Integer> walls = new HashMap<Player,Integer>();
    
    int currentWalls = walls.get(player);
    walls.put(player, currentWalls-1);
    

    【讨论】:

    【解决方案2】:

    我会使用AtomicInteger 来保存您的价值观。它是线程安全的,以防多个玩家同时撞墙。而且它比每次都重新创建一个新的 Integer 更简单(如@Jack 回答)

    HashMap<Player, AtomicInteger> walls = new HashMap<Player,AtomicInteger>();
    
    ...
    
    walls.get(player).decrementAndGet();
    

    如果需要,您可以从 decrementAndGet() 调用中返回值以检索新的墙数。

    【讨论】:

    • 您好,感谢您的回复。我现在通过使用整数键和值的映射来完成这项工作,但是您的 AtomicInteger 想法对我来说很有趣而且很新。所以我在我的初始化中创建了一个循环来填充我的墙壁地图,但是当我做 wall.put(i, numwalls) 时,我在 numWalls 下得到一个错误,说它需要一个 AtomicInteger,而不是一个 Integer。我有点困惑,但我喜欢你的想法
    • Java 会将 int “自动装箱”为 Integer,但不会自动装箱为 AtomicInteger。在你的循环中去walls.put(theKey, new AtomicInteger(numwalls));
    【解决方案3】:

    要更改使用键存储的值,您应该删除旧值并添加一个新值。

    也就是说,您是否考虑过创建一个 Player 类来封装 playerId 和玩家拥有的墙数?它可能更适合您的计划。

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多