public ArrayList(Collection<? extends E> c)方法的一个注释 c.toArray might (incorrectly) not return Object[] (see 6260652)
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
* 使用集合类对象初始化ArrayList
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
//刚才看了一眼这个toArray方法,确实有时候返回的不是Obdect[],
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
以上为ArrayList的一个构造函数,在里面有一句
c.toArray might (incorrectly) not return Object[] (see 6260652)
这个注释,参见bug6260652,java的bug官网http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6260652
问题就在这儿了,List<String> strList= Arrays.asList("abc");返回的结果是String[],不谢。。。
以下为测试代码
1 public static void main(String[] args) { 2 List<String> stringList= Arrays.asList("test"); 3 Object[] strList=stringList.toArray(); 4 System.out.println(strList.getClass()); 5 }
结果如下:

邮箱:akgang@126.com
非著名程序猿-akg

浙公网安备 33010602011771号