[英]Using Integer in HashMaps
So I am trying to use HashMaps by specififying: 所以我试图通过指定使用HashMaps:
HashMap totalAtt = new HashMap<String, Integer>();
But when I try to add two of the Integers, it gives me an error saying bad operand. 但是,当我尝试添加两个Integer时,它给我一个错误,提示操作数错误。 How can I add the integers I retrieve from this HashMap without a compiler error or warning?
如何在没有编译器错误或警告的情况下添加从此HashMap中检索到的整数?
Edit: Replaced some code, no longer getting compiler error, rather warning of unchecked or unsafe operations 编辑:替换了一些代码,不再得到编译器错误,而是警告未检查或不安全的操作
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;
}
From Sprite class: 从Sprite类:
public HashMap<String, Integer> getAttrib()
{
return attrib;
}
Change 更改
HashMap totalAtt = new HashMap<String, Integer>();
to 至
HashMap<String, Integer> totalAtt = new HashMap<>();
and 和
HashMap sAtt
to 至
HashMap<String, Integer> sAtt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.