明明设置了目的List的大小,为什么执行Collections.copy()还是报错:Source does not fit in dest

首先说一下,报错Source does not fit in dest。

在复制List时,使用Collections.copy(dest, src)方法,首先会检查src的大小是否大于dest的大小,如果大于,则报错。

这一点,源码写的很清楚:

    /**
     * Copies all of the elements from one list into another.  After the
     * operation, the index of each copied element in the destination list
     * will be identical to its index in the source list.  The destination
     * list must be at least as long as the source list.  If it is longer, the
     * remaining elements in the destination list are unaffected. 

* * This method runs in linear time. * * @param dest The destination list. * @param src The source list. * @throws IndexOutOfBoundsException if the destination list is too small * to contain the entire source List. * @throws UnsupportedOperationException if the destination list's * list-iterator does not support the set operation. */ public static void copy(List dest, List src) { int srcSize = src.size(); if (srcSize > dest.size()) //在这里判断大小 throw new IndexOutOfBoundsException("Source does not fit in dest"); if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) { for (int i=0; i di=dest.listIterator(); ListIterator si=src.listIterator(); for (int i=0; i

但是,如果给dest的List设置了大小,比如下面这样,为什么还是报错?

List dest = new ArrayList(src.size());
Collections.copy(dest, src);

实际上,这样传入的size,只分配了内存,却没有定义元素。

如果这时候打印dest的size,得到的是0。

 

怎么办?

当你百度如何深拷贝List时,你可能会看到以下两种写法

List dest = Arrays.asList(new String[src.size()]);
CollectionUtils.addAll(dest, new Object[src.size()]);

其本质都是把dest撑起来,此时再执行copy就没有问题了。

你可能感兴趣的:(Java,java)