Remove all elements from a List after a particular index

list.subList(4, list.size()).clear();

Sublist operations are reflected in the original list, so this clears everything from index 4 inclusive to list.size() exclusive, a.k.a. everything after index 3. Range removal is specifically used as an example in the documentation:

This method eliminates the need for explicit range operations (of the
sort that commonly exist for arrays). Any operation that expects a
list can be used as a range operation by passing a subList view
instead of a whole list. For example, the following idiom removes a
range of elements from a list:

     list.subList(from, to).clear();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)