-
背景问题
一直搞不清楚 ArrayList 的实现方式今天用debug看了系统类库的解决了问题. -
类库代码
private void add(E e, Object[] elementData, int s) {
if (s == elementData.length) {
elementData = this.grow();
}
elementData[s] = e;
this.size = s + 1;
}
public boolean add(E e) {
++this.modCount;
this.add(e, this.elementData, this.size);
return true;
}
- 分析总结
这里向ArrayList中添加元素的本质上就是向Object类型的数组中添加元素, 通过指定ArrayList的泛型来限制Object [ ]数组中元素的类型 .