项目代码片段:
public List getOrderDetailList() {
List orderDetailList = new ArrayList();
Collections.copy(orderDetailList, orderDetails);
return orderDetailList;
}
OrderDetailPo 是父类,orderDetailList集合用于存放OrderDetailPo集
OrderDetailBo是子类,orderDetails集合用于存放OrderDetailBo集
即将子类集合拷贝为父类集合
查看Collections.copy(List
public static void copy(Listsuper T> 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; ielse {
ListIteratorsuper T> di=dest.listIterator();
ListIterator si=src.listIterator();
for (int i=0; i
可以看出通过比较目标集合和源集合长度大小,目标集合长度必须大于源集合长度才能进行拷贝,反之将抛出IndexOutOfBoundsExceptin异常
拷贝时通过迭代器遍历集合,通过set方法赋值
上述使用时抛出InexOutOfBoundsExceptin异常的原因即orderDetailList的长度小于orderDetails,因为orderDetailList是新实例化的集合,默认长度为0
试着在orderDetailList实例化时指定长度
public List getOrderDetailList() {
List orderDetailList = new
ArrayList(orderDetails.size()+1);
Collections.copy(orderDetailList, orderDetails);
return orderDetailList;
}
这里在实例化orderDeteailList时指定长度为orderDetails.size()+1,保证目标集合长度大于源集合长度
可是还是抛出IndexOutOfBoundsExceptin异常
通过检查发现,orderDeteailList的长度依然为0,为什么呢?
查询ArrayList构造函数:
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_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. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException
("Illegal Capacity: " + initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection 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);
}
可以看出虽然我们使用ArrayList(int initialCapacity)构造函数实例化,构造函数将实例化指定长度数组的elementData用于将来ArrayList存放元素
但是并没有改变size属性变量的值,size依旧是默认值0
public int size() {
return size;
}
通过集合size() 方法获取集合长度依旧是0,故抛出上述IndexOutOfBoundsExceptin异常
再看集合的add方法:
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return true (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
可以发现在每次add元素时,size将进行变化,ensureCapacityInternal是判断是否需要进行扩容操作
回过头看ArrayList的另一个构造函数:
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection 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);
}
该方法会改变size的值,然后使用数组拷贝的方式进行集合元素的拷贝
故直接使用ArrayList的构造函数即可实现集合的拷贝:
public List getOrderDetailList() {
List orderDetailList = new
ArrayList(orderDetails);
return orderDetailList;
}