Lists
Lists keep items in order. An ArrayList is fast at reading by position, and a LinkedList is fast at adding and inserting.
Apprentice · Java · Updated
You have already used lists throughout this course. Lists are the most common type of collection used in programming. You should be comfortable working with lists.
Lists are ordered, meaning we can access their items by ordinal position.
All lists can add, insert, and remove items. Different types of lists store the data differently.
ArrayList
ArrayLists store the data as arrays, which makes them efficient at getting values by position. The primary benefit is quick data access.

LinkedList
LinkedLists store each item separately from other items. Each item has a pointer that references the next item in the list. The primary benefit is the ability to add and insert new items quickly.

Do it: seat guests on the restaurant's waitlist
A busy restaurant keeps a waitlist of names, first in line at position 0. Guests who walk in join the end of the line. Guests with a reservation skip the line and go to the front. Guests join all evening, so the waitlist is a LinkedList, the list that's quick at adding and inserting.
The method gets three values:
waitlist: the names waiting, aList<String>, first in line at position 0.guest: the name of the guest who just arrived, such as"Noah".hasReservation:truewhen the guest has a reservation.
It changes waitlist and returns nothing.
For example:
List<String> waitlist = new LinkedList<>(List.of("Ava", "Liam"));
addGuest(waitlist, "Noah", true);
addGuest(waitlist, "Mia", false);
| Step | Has a reservation? | The waitlist afterward |
|---|---|---|
| The start | Ava, Liam | |
addGuest(waitlist, "Noah", true) |
yes: to the front | Noah, Ava, Liam |
addGuest(waitlist, "Mia", false) |
no: to the end | Noah, Ava, Liam, Mia |
So waitlist ends up as ["Noah", "Ava", "Liam", "Mia"].
Your job: write the method addGuest(List<String> waitlist, String guest, boolean hasReservation).
- A guest with a reservation goes to the front: position 0.
- A guest without one goes to the end.
- The guests already waiting keep their order.
- An empty waitlist gets the guest as its only name, either way.
Practiced live on the lesson page: write the Java right there and check it.