我在显示 ht.keySet()
的行中收到以下错误:
Type mismatch: cannot convert from element type Object to int
ht
是 LinkedHashMap
.
for (int key : ht.keySet())
{
if(ht.get(key).size() == 0)
{
System.out.println("There is no errors in " + key) ;
}
else
{
System.out.println("ERROR: there are unexpected errors in " + key);
}
}
最佳答案
您需要使用 Java generics .
声明ht
作为LinkedHashMap<Integer, Foo>
其中 Foo
是您希望 ht.get()
返回的任何数据类型.使用 Map
界面会更好:
LinkedHashMap<Integer, Foo> ht = new LinkedHashMap<Integer, Foo>();
// or preferably
Map<Integer, Foo> ht = new LinkedHashMap<Integer, Foo>();
关于java - 类型不匹配 : cannot convert from element type Object to int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106173/