Simple understanding of ArrayList source code

ArrayList in Java Collection Framework

1. ArrayList is dynamically expanded, each expansion is 1.5 times the original length, and a lazy loading strategy is adopted. When the add method is called for the first time, the array will be expanded to 10 (default value)

2. Simulation implementation

List interface:

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, which is implemented by myself, initializes the size of the array to 10 in the construction method. In the source code, the size is expanded to 10 when the add() is called for the first time (the source code is an array with a default length of 0 defined by the array reference in the construction method. );

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);
    }
}

test

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]

    }

}

Guess you like

Origin blog.csdn.net/Beer_xiaocai/article/details/104902028