public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
    elementData = Arrays.copyOf(elementData, size, Object[].class);
    }
    看java源码时,ArrayList的构造函数里面为什么会有这样的一句话,c.toArray might (incorrectly) not return Object[] (see 6260652),首先为什么c.toArray()不能返回Object[] ,后面说see 6260652,又怎样才能看到?java源码ArrayList构造函数

解决方案 »

  1.   

    6260652
    是他们内部的编号,估计是个bug的编号,找到这个就知道原因了
    到官方网站去搜索
    LZ研究的很深哦,顶一个
      

  2.   

    这个你要去看toArray()是怎么实现的,这样写就是说toArray这个方法不一定在任何情况都可以转化
    会出现转化失败,然后后面的if是用来补救的,当toArray()转失败了就用copyOf()来转
    see后面的应该是bug代码我猜,应该是还没找到原因,所以才用这两层的转化
      

  3.   

    1、elementData = c.toArray();返回的就是对象数组,elementData 就是对象数据
     /**
         * The array buffer into which the elements of the ArrayList are stored.
         * The capacity of the ArrayList is the length of this array buffer.
         */
        private transient Object[] elementData;2、// c.toArray might (incorrectly) not return Object[] (see 6260652)
    由于toArray使用了泛型,说的是不正确的情况可能不返回对象数组。至于see 6260652 研究他毫无意义。
      

  4.   

    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
        elementData = Arrays.copyOf(elementData, size, Object[].class);
        }
    c.toArray might (incorrectly) not return Object[] (see 6260652)书上说的是可能不会(或不正确地)返回Object[],没有说不可能!你对英语语法理解不对。if (elementData.getClass() != Object[].class)
        elementData = Arrays.copyOf(elementData, size, Object[].class);
        }
    上面这段代码的作用就是当c.toArray()不会或不正确地返回Object[]时,对c.toArray()进行修正,让它返回成Object[]
    至于那个see 6260652可能是人家内部人员研究或改动代码时方便查询用的
      

  5.   

    see 6260652 表示 JDK bug 编号,前面数字“6”表示是在 JDK 6 上发现的。Sun/Oracle JDK BUG 6260652 URL 为:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6260652