to a sublist and can cause undefined results (so don't do it). Sublists allow
you to do anything to part of a list that you could to do an entire list, so they
can be a powerful tool. For example, you can remove part of a list using
list.subList(min,max).clear().
public ListIterator<E>listIterator(int index)
Returns a ListIterator object that will iterate through the elements of
the list starting at the indexth entry.
public ListIterator<E>listIterator()
Returns a ListIterator object that will iterate through the elements of
the list starting at the beginning.
All the methods that take an index will throw IndexOutOfBoundsException if index is less than
zero or greater than or equal to the list's size.
The java.util package provides two implementations of ListArrayList and LinkedList.
21.6.1. ArrayList
ArrayList is a good basic list implementation that stores its elements in an underlying array. Adding and
removing elements at the end is very simple0(1). Getting the element at a specific position is also 0(1).
Adding and removing elements from the middle is more expensive0(n-i) where n is the size of the list and i is
the position of the element being removed. Adding or removing the element requires copying the remainder of
the array one position up or down.
An ArrayList has a capacity, which is the number of elements it can hold without allocating new memory
for a larger array. As you add elements they are stored in the array, but when room runs out, a replacement
array must be allocated. Setting your initial capacity correctly can improve performance. If the initial size of
the data is significantly smaller than its final size, setting the initial capacity to a larger value reduces the
number of times the underlying array must be replaced with a larger copy. Setting the size too large can waste
space.
ArrayList has three constructors:
publicArrayList()
Creates a new ArrayList with a default capacity.
publicArrayList(int initialCapacity)
Creates a new ArrayList that initially can store initialCapacity
elements without resizing.
publicArrayList(Collection<? extends E> coll)
Creates a new ArrayList whose initial contents are the contents of coll.
The capacity of the array is initially 110% of the size of coll to allow for
some growth without resizing. The order is that returned by the collections
iterator.