java - 在 HashMap 中使用整数

标签 java

所以我尝试通过指定来使用 HashMap:

HashMap totalAtt = new HashMap<String, Integer>();

但是当我尝试将两个整数相加时,它给我一个错误,指出操作数错误。如何添加从此 HashMap 检索的整数而不出现编译器错误或警告?

编辑:替换了一些代码,不再出现编译器错误,而是警告未经检查或不安全的操作

public HashMap<String, Integer> getAttrib()
{
    HashMap<String, Integer> totalAtt = new HashMap();

    //Creates key values and initializes them to 0
    totalAtt.put("strength", 0);
    totalAtt.put("dexterity", 0);
    totalAtt.put("constitution", 0);
    totalAtt.put("intelligence", 0);
    totalAtt.put("wisdom", 0);
    totalAtt.put("charisma", 0);

    HashMap<String, Integer> sAtt;

    for(Sprite s: itemList)
    {
        //iterates through items and counts their attributes
        sAtt = s.getAttrib();

        totalAtt.put("strength", totalAtt.get("strength") + sAtt.get("strength"));
        totalAtt.put("dexterity", totalAtt.get("dexterity") + sAtt.get("dexterity"));
        totalAtt.put("constitution", totalAtt.get("constitution") + sAtt.get("constitution"));
        totalAtt.put("intelligence", totalAtt.get("intelligence") + sAtt.get("intelligence"));
        totalAtt.put("wisdom", totalAtt.get("wisdom") + sAtt.get("wisdom"));
        totalAtt.put("charisma", totalAtt.get("charisma") + sAtt.get("charisma"));
    }

    return totalAtt;
}

来自 Sprite 类:

public HashMap<String, Integer> getAttrib()
{
    return attrib;
}

最佳答案

改变

HashMap totalAtt = new HashMap<String, Integer>();

HashMap<String, Integer> totalAtt = new HashMap<>();

HashMap sAtt

HashMap<String, Integer> sAtt

关于java - 在 HashMap 中使用整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658300/

相关文章:

java - 一次将一个 View 添加到后台线程中的线性布局,而不是在所有操作完成后

java - 简单可靠的内存数据库,用于快速 Java 集成测试,支持 JPA

java - 对象类型可以由它的父类(super class)定义吗?

java - 在 HTTP POST 中发布什么内容

Java 日志记录说明

java - 如何使用 Java 中的 API Update(版本 5)更新 ElasticSearch 中的 HashMap 数据?

java - 通过检查其模式来验证字符串

java - 在 Javac 命令中导入 makefile 中的多个库

java - 处理许多记录的一般事务问题

javascript - 如何在不刷新/提交页面的情况下发送 POST 方法