[Source] Read ArrayList

Copyright: https://blog.csdn.net/qq_21852449/article/details/85225368
  1. Data structure
    sequential structure
    Here Insert Picture Description
  2. Inheritance & implementation class
    Here Insert Picture Description
  3. Class of property
//列表最大长度
private static final int MAX_ARRAY_SIZE = 2147483639;
//存放数据的数组,不会序列化
transient Object[] elementData;
//默认长度  10
private static final int DEFAULT_CAPACITY = 10;
//结构性修改数
protected transient int modCount = 0;
  1. Constructor of the class
//传入一个ArrayList新建一个ArrayList
public ArrayList(Collection<? extends E> var1) {...}

public ArrayList(int var1) {...}

public ArrayList() {}
  1. Kernel
    5.1 add (E e) {... }
    call grow () {...} expansion method, each time the current is 0.5 times the length of expansion of
    5.2 SET (int index, E E) {...}
    5.3 the indexOf (Object var1) {... }
public int indexOf(Object var1) {
    int var2;
    if (var1 == null) {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (this.elementData[var2] == null) {
                return var2;
            }
        }
    } else {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (var1.equals(this.elementData[var2])) {
                return var2;
            }
        }
    }

    return -1;
}
  1. Summary
    ArrayList has its special application scenarios, and LinkedList, respectively. The advantage is random read, the disadvantage of requiring the insertion elements move macronutrient, efficiency is not high.
    Further ArrayList AbstractCollection inheritance can be switched between different structures by the constructor.

Guess you like

Origin blog.csdn.net/qq_21852449/article/details/85225368