As per the JavaDoc
on the matter:
List subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex,
exclusive. (If fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and
vice-versa. The returned list supports all of the optional list
operations supported by this list.
The sub list will point to the same elements present within the original list, thus, any changes made through the sub list will be reflected within the original list since you are changing the same objects.
EDIT: As per your comment, assume that the original list
has the following references: 0x00 0x01 0x02 0x03 0x04 0x05
and these map to locations in memory where objects exist.
Doing sublist(0, 2)
on the above will yield a list which contains pointers to the following memory locations 0x00 0x01 0x02
which are the same as in original list
.
What this means is that if you do sublist.get(0).setFoo(foo)
, this will in turn seek out the object present at 0x00
and set some property. However, 0x00
is also being referenced to by original list
, which is why changing the sub list means that you will be changing the source list since both lists point to the same objects. The same also holds should you change your elements through original list
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…