1
我有someMethod()
它返回Collection<Long>
。奇怪的行爲,通用ArrayList的長
ArrayList<Long> results = (ArrayList<Long>) someMethod();
Long value = results.get(0);
我得到ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long
。實際上,它返回例如 嘗試System.out.println(results.get(0));
。 [32]
。 我不明白這一點。這是Long的ArrayList!爲什麼get(0)
返回ArrayList
?
這有助於:
Object o = results.get(0);
ArrayList<Long> al = (ArrayList<Long>) o;
Long val = al.get(0);
偏偏這個需要?
'someMethod'返回'Collection>'。 ''[32]「'等同於'ArrayList'的'ArrayList.toString()',只包含'32' –
oldrinb
我們可以看到'someMethod'的簽名嗎? –
'Collection someMethod(Collection <?extends SomeInterface> target)' –
LancerX