【发布时间】:2012-08-26 19:33:15
【问题描述】:
我有someMethod(),它返回Collection<Long>。
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]。
我不明白。这是长的 ArrayList!为什么get(0) 返回ArrayList?
这有帮助:
Object o = results.get(0);
ArrayList<Long> al = (ArrayList<Long>) o;
Long val = al.get(0);
但为什么需要这样做?
【问题讨论】:
-
someMethod返回Collection<ArrayList<Long>>。"[ 32 ]"等价于ArrayList.toString()的ArrayList仅包含32 -
能看到
someMethod的签名吗? -
Collection<Long> someMethod(Collection<? extends SomeInterface> target)