The collections framework provides a core set of interfaces to define different collection behaviors. One of the core interfaces is the List
interface. A List
is a collection where elements are ordered in a sequence. List
s allow us to have duplicate elements and fine-grain control over where elements are inserted in the sequence. Like arrays, the position of a List
is known as the index and is 0
based. Unlike arrays, which have a static size, List
s are dynamically sized.
The collections framework provides many List
implementations, but we’ll focus on the ArrayList
and LinkedList
. The ArrayList
is the overall preferred implementation for most use cases but the LinkedList
performs better than an ArrayList
if your program mostly inserts and deletes elements at the beginning or end of a list.
Let’s create a List
using an ArrayList
as its implementation and see some operations:
List<Integer> intList = new ArrayList<>(); // Empty `List` intList.add(4); // 4 intList.add(6); // 4, 6 intList.add(3); // 4, 6, 3 intList.set(1, 3); // 4, 3, 3 int a = intList.get(2); // a = 3 int b = intList.indexOf(3); // b = 1 List<Integer> subIntList = intList.subList(1,3); // subIntList -> 3, 3
In the example above, we:
- Created a
List
reference namedintList
with anArrayList
implementation. - Called
add()
, which appends elements to the end of theList
. We can see the state ofintList
after each call. - Called
intList.set(1, 3)
, which replaces the element at index1
(the second element in 0-based indexing) with3
. - Called
get()
, which gets the value at index2
(the third element in 0-based indexing). - Called
indexOf()
, which returns the index of the first occurrence of3
(the first3
is at index1
). - Called
subList()
, which returns a sublist in a newList
with the elements specified by the starting index1
(inclusive) and ending index3
(exclusive).
We can iterate through a List
using the enhanced for-loop
. For example:
// Assuming `intList` has elements -> 1, 5, 2, 6, 1 for (Integer number: intList) { System.out.println(number); } // OUTPUT TERMINAL: 1 5 2 6 1
In the example above, we used the enhanced for-loop
, which iterates through the elements in intList
from index 0
to the end of the list. Note that we use the int
wrapper class Integer
to iterate through the elements in intList
. However, we could have also used an ‘int’.
Let’s practice creating a List
and iterating through it.
Instructions
Let’s create a collection of words that are in sequential order.
In main()
of Main.java, create a String
type List
reference with an ArrayList
implementation named stringList
.
Let’s add elements in our collection.
In main()
, call add()
on stringList
so that stringList
has the following sequence of elements: "Hello", "World", "!"
Let’s print out all the elements in our List
.
After adding String
s to stringList
, iterate through stringList
with an enhanced for-loop
using a String
reference named element
. In the body of the for-loop
call System.out.println()
and output element
to the terminal.