如何更新 ArrayList<HashMap<String, ?>>
?
数量为String
values.get(position).put(KEY_POSITION, quantity);
错误:
The method put(String, capture#11-of ?) in the type HashMap is not applicable for the arguments (String, String)
最佳答案
所以问题与通配符的逻辑有关。通配符的意思是“值类型参数可以是任何东西”,所以 HashMap<String, ?>
是有效的,但在这里您无法将字符串值放入其中。
注意: HashMap<String, ?>
与 HashMap<String, ? extends Object>
相同所以 ?
可以是任何类型的类,但 String 不能是任何类型的类。这就是您的错误的原因。
您需要做一些小改变:
HashMap<String, ? super String>
或者按照@user902383的建议直接将1(结果相同)更改为:
HashMap<String, String>
现在它将接受字符串。
1 String 类是最终的(你不能从它扩展) - 因此最好使用 HashMap<String, String>
第一种方法是 HashMap<String, ? super String>
是“无用的”,因为您无法从 String 类扩展。
关于java - 更新 ArrayList<HashMap<String, ?>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23107181/