private transient Object[] elementData;
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
this.elementData = new Object[initialCapacity]; //生成一个长度为10的Object类型的数组
}
public ArrayList() {
this(10); //调用ArrayList(int i)
}<br><br>
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray(); //返回包含此 collection 中所有元素的数组
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class); //复制指定的数组,返回包含相同元素和长度的Object类型的数组
}
public boolean add(E e) {
ensureCapacity(size + 1); // 扩大数组容量
elementData[size++] = e; //将元素e添加到下标为size的Object数组中,并且执行size++
return true;
}
public void add(int index, E element) {
if (index > size || index < 0) //如果指定要插入的数组下标超过数组容量或者指定的下标小于0,抛异常
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
ensureCapacity(size+1); // 扩大数组容量
System.arraycopy(elementData, index, elementData, index + 1,size - index); //从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。<br> // elementData --- 源数组 index --- 源数组中的起始位置 <br> // elementData --- 目标数组 index+1 --- 目标数组中的起始位置<br> // size - index --- 要复制的数组元素的数量
elementData[index] = element; //将要添加的元素放到指定的数组下标处
size++;
}
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length; //原数组的容量
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1; //定义新数组的容量,为原数组容量的1.5倍+1
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity); //复制指定的数组,返回新数组的容量为newCapacity
}
}
public E get(int index) {
RangeCheck(index); //检查传入的指定下标是否合法
return (E) elementData[index]; //返回数组下标为index的数组元素
}
private void RangeCheck(int index) {
if (index >= size) //如果传入的下标大于或等于集合的容量,抛异常
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
public E remove(int index) {
RangeCheck(index); //检查指定的下标是否合法
modCount++;
E oldValue = (E) elementData[index]; //获取指定下标的数组元素
int numMoved = size - index - 1; //要移动的元素个数
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved); //移动数组元素
elementData[--size] = null; // Let gc do its work
return oldValue;
}
public boolean remove(Object o) {
if (o == null) { //如果传入的参数为null
for (int index = 0; index < size; index++)
if (elementData[index] == null) { //移除首次出现的null
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) { //移除指定位置的元素,实现方法类似remove(int i)
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone(); //调用Object类的clone方法返回一个ArrayList对象
v.elementData = Arrays.copyOf(elementData, size); //复制目标数组
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有