Java集合框架中ArrayList
1.ArrayList是动态扩容的,每次扩容为原来长度的1.5倍,并且采用的是懒加载策略,当第一次调用add方法时,数组才会扩容为10(默认值)
2.模拟实现
List接口:
public interface List<E> {
int size();
void add(E e);
E get(int index);
E set(int index, E e);
boolean remove(int index);
}
自己实现的MyArrayList在构造方法中数组就初始化大小为10,源码中在第一次调用add()时大小才扩容为10(源码中是在构造方法中数组引用定义好的默认长度为0的数组);
public class MyArrayList<E> implements List<E> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elementData;
private int size;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
@Override
public int size() {
return size;
}
@Override
public void add(E e) {
ensureCapacity(size + 1);
elementData[size++] = e;
}
@Override
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
@Override
public E set(int index, E e) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = e;
return oldValue;
}
@Override
public boolean remove(int index) {
rangeCheck(index);
/*for (int i = index + 1; i < size; i++) {
elementData[i - 1] = elementData[i];
}*/
System.arraycopy(elementData,index+1,elementData,index,size-index-1);
elementData[--size] = null;
return true;
}
//动态扩容
private void ensureCapacity(int minCap) {
if (minCap - elementData.length > 0) {
group(minCap);
}
}
private void group(int minCap) {
int oldCap = elementData.length;
int newCap = oldCap + (oldCap >> 1);
if (newCap - minCap < 0) {
newCap = minCap;
}
if (newCap - (Integer.MAX_VALUE - 8) > 0) {
throw new IllegalArgumentException("长度超出");
}
elementData = Arrays.copyOf(elementData, newCap);
}
//检查下标是否越界
private void rangeCheck(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException("下标越界");
}
}
//将Object类型的元素转为E
@SuppressWarnings("unchecked")
private E elementData(int index) {
return (E) elementData[index];
}
@Override
public String toString() {
Object[] res = Arrays.copyOf(elementData,size);
return Arrays.toString(res);
}
}
测试
public class Test {
public static void main(String[] args) {
MyArrayList<Integer> list = new MyArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(1);
list.add(2);
list.add(3);
list.add(1);
list.add(2);
list.add(3);
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);//[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
System.out.println(list.get(0));//1
System.out.println(list.size());//12
list.set(10,0);
System.out.println(list);//[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 0, 3]
list.remove(11);
System.out.println(list.size());//11
System.out.println(list);//[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 0]
}
}