Person[] p = new Person[10];//Person是自定义的一个类型
ArrayList<Person> pList = new ArrayList<Person>();
上边的p跟pList都能用来存放person类型的数据,怎么选择?ArrayList可以实现的功能数组也能实现,为啥会有ArrayList呢?
1 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
ArrayList底层就是Object数组
transient Object[] elementData
不过它可以动态扩容,这是它的扩容方法
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
添加回答
举报
0/150
提交
取消