我有這樣的功能:Java泛型:需要捕獲#67的?
/**
* Helper function that adds the values of b to the elements of a, treating
* all keys that exist in b but not in a, as existent in a with value 0. NB:
* It operates IN PLACE.
* @param a The {@link java.util.Map} which will hold the result
* @param b The {@link java.util.Map} which will be added to a
*/
private static void sumMaps(Map<?, Integer> a, Map<?,Integer> b)
{
for (Object key : b.keySet()) {
Integer currentCount = a.get(key);
a.put(key, currentCount == null ? b.get(key) : currentCount + b.get(key));
}
}
不過,NetBeans突出「重點」中的最後一行,並給了我這個錯誤:
method put in class java.util.Map<K,V> cannot be applied to given types
required: capture #67 of?, java.lang.Integer
found: java.lang.Object, int
(int是沒有問題的,因爲Java解包,我也試過使用整數,但它沒有工作)。
謝謝大家的回答,他們都提出了大致相同的事情,所以我隨機抽取了一個。 – FrontierPsycho 2010-07-20 13:51:44
注意'Map.entrySet'會給你一個更有效的迭代方式。 – 2010-07-20 14:47:43