为什么这段代码抛出异常
Why does this code throws exception
HashMap h = new HashMap();
Collection c = h.values();
Object[] a = c.toArray();
LinkedList<Object[]> l = new LinkedList<Object[]>();
l.addFirst(a);
TreeSet<Object[]> t = new TreeSet<Object[]>(l); //throws ClassCastException exception
因为我没有违反任何合同,所以这个异常很奇怪。
TreeSet 构造函数抛出 ClassCastException if a collection is passed whose elements do not all implement Comparable. Object[] does not implement Comparable。
详情请参阅constructor reference。
ClassCastException - if the elements in c are not Comparable, or are not mutually comparable
NullPointerException - if the specified collection is null
HashMap h = new HashMap();
Collection c = h.values();
Object[] a = c.toArray();
LinkedList<Object[]> l = new LinkedList<Object[]>();
l.addFirst(a);
TreeSet<Object[]> t = new TreeSet<Object[]>(l); //throws ClassCastException exception
因为我没有违反任何合同,所以这个异常很奇怪。
TreeSet 构造函数抛出 ClassCastException if a collection is passed whose elements do not all implement Comparable. Object[] does not implement Comparable。
详情请参阅constructor reference。
ClassCastException - if the elements in c are not Comparable, or are not mutually comparable NullPointerException - if the specified collection is null