There is a method in Java that returns a sublist:
public list<E> subList(int fromIndex, int toIndex){ subListRangeCheck(fromIndex, toIndex,size); returnnew SubList(this , 0, fromIndex, toIndex); }Copy the code
Returns a sublist with fromIndex as the starting point and toIndex as the ending point (not including the ending point). As you can see from the above implementation code, check whether the fromIndex and toIndex are valid, if not, then exit the function logic. If it is, you can see that it returns a this, which is a partial view of the elements of the original list, forming a sublist.
As a result, any changes to the original list or to the list returned by sublist (where the changes are not related to the list size) are made to the same memory. (This type of modification is called unstructural modification)
If the original list is being structurally modified (excluding changes due to returned sublists), the returned sublist will be semantically undefined. In AbstractList (ArrayList superclass), is the specific manifestation of the undefined throws a ConcurrentModificationException. If you change the size of the sublist after calling sublist, the sublist will be invalidated and become unusable.
Special note: the white-box testing section using Sublist focuses on the above situations.