📜  ClassCastException 强制转换 toArray() 方法 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:42.751000             🧑  作者: Mango

代码示例1
Collection c = new ArrayList();
Integer obj = new Integer(1);
c.add(obj);

    // this would trigger the rule (and throw a ClassCastException if executed)
Integer[] a = (Integer [])c.toArray();

   // this is fine and will not trigger the rule
Integer[] b = (Integer [])c.toArray(new Integer[c.size()]);