所以我想通過specififying使用包含HashMap:中包含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'引用變量可能不是一個好主意。 – rgettman
也許只是用'HashMap totalAtt = new HashMap <>();'替換'HashMap totalAtt = new HashMap ();'。 –
DavidS
@rgettman增加了更多代碼,當前錯誤來自: totalAtt.put(「strength」,totalAtt.get(「strength」)+ sAtt.get(「strength」)); –