Examining List and ArrayList
List
is an interface implemented by the ArrayList
class. Therefore, any API method in List
is automatically in ArrayList
. As we know, it is good coding practice to use an interface reference (List
) to refer to an object (ArrayList
). As the compiler looks at the reference type, this frees you up in the future to use different implementations of List
, such as LinkedList
.
Both List
and ArrayList
are in the java.util
package. In the API, both are typed generically with E
(for Element
), which means we are free to specify the type we want to store in our list. Failure to follow the declared type results in a compiler error. We will cover generics in detail in Chapter 13.
List properties
A list is an ordered collection (sometimes called a sequence). We have precise control over where in the list an element is inserted. Indices (as with arrays) start at 0 and duplicate elements are allowed. The order that lists maintain is insertion order. In other words...