ArrayList parse the source code of SubList

ArrayList is subList inner class,

public List<E> subList(int fromIndex, int toIndex) {
    subListRangeCheck(fromIndex, toIndex, size);
    return new SubList(this, 0, fromIndex, toIndex);
}

subListRangeCheck check whether the index is out of bounds.

Key Constructors subList

SubList(AbstractList<E> parent,
        int offset, int fromIndex, int toIndex) {
    this.parent = parent;
    this.parentOffset = fromIndex;
    this.offset = offset + fromIndex;
    this.size = toIndex - fromIndex;
    this.modCount = ArrayList.this.modCount;
}

List enough into the array.

this parameter is referring to the List object that external calls subList methods, other are very good understanding. Note that subList place is the epitome of an external list as long as the changes in the external value of the list sublist will also change, a shared memory variables.

It is generally used subList best not to change the original list, generally only read operations are allowed.

 

 

Published 55 original articles · won praise 31 · views 80000 +

Guess you like

Origin blog.csdn.net/zengfanwei1990/article/details/90704883