我有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);
但为什么需要这样做?