Core Java · Chapter 27 of 48

Lists

By the end of this chapter you can keep things in a list that grows and shrinks, remove from it safely while you loop, and hand it out without letting anyone change it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 6 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

The Rajdhani stops at NDLS for ten minutes, and in those ten minutes the counter takes the order for a whole coach. Ravi says passengers add things, cancel things, and the pantry car wants its tea first. He needs an order that can do all of that without losing a cup, and a menu that nobody changes by accident.

With what you know so far, you would use an array, which has a fixed length: to add an item you copy everything into a bigger array, and to cancel one you shift every later item by hand. One wrong index and the wrong cup is cancelled.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    OrderList.java: add, rush and cancel items

    In OrderList.java, write three methods on the items list. add(item) puts the item at the end. rush(item) puts it at the front, so the pantry makes it first. cancel(item) removes only the first occurrence of the item and returns true; when the item is not in the order it returns false and changes nothing.

    For example, adding Masala chai and Bun maska, then rushing Cutting chai, gives Cutting chai, Masala chai, Bun maska. Add another Masala chai and cancel Masala chai: it returns true and leaves Cutting chai, Bun maska, Masala chai. Cancelling Parle-G returns false and 3 items remain.

  2. Task 2

    OrderList.java: hand out the items as a read-only copy

    In OrderList.java, make items() return the items in order as a read-only copy, not the list the order keeps. Adding to the returned list throws an UnsupportedOperationException. A copy taken earlier does not change when the order changes later.

    For example, take the items of an order holding Masala chai, then add Bun maska to the order: the earlier list still holds only Masala chai, and a new call to items() returns Masala chai, Bun maska. The check fills the order with add from task 1, so do task 1 first.

  3. Task 3

    OrderList.java: cancel every cup of one item

    In OrderList.java, make cancelAll(item) remove every occurrence of the item from the order, keep the other items in their order, and return how many were removed. When nothing matches it returns 0 and changes nothing. Matches side by side and a match at the very end must all go.

    For example, on Masala chai, Masala chai, Bun maska, Masala chai, Cutting chai, Masala chai, cancelAll of Masala chai returns 4 and leaves Bun maska, Cutting chai; cancelAll of Parle-G then returns 0. The check fills the order with add from task 1, so do task 1 first.

  4. Task 4

    Menu.java: remove a price by value, not by position

    In Menu.java, make withoutPrice(prices, price) return a new list holding the prices without the first occurrence of the value price. The given list is not changed (it may be unmodifiable). When the price is not in the list, the new list holds the same prices.

    For example, 40, 25, 50, 25 without 25 gives 40, 50, 25, and the given list is still 40, 25, 50, 25. 10, 0, 40 without 0 gives 10, 40: 0 is a free water's price, not index 0. 40, 1, 40 without 40 gives 1, 40. 40, 25 without 99 gives 40, 25.

  5. Task 5

    Menu.java: add a special to a menu that cannot be changed

    In Menu.java, make withSpecial(menu, special) return a new list holding every item of the menu in order, then the special at the end. The menu may be unmodifiable (made with List.of) and must stay unchanged. The returned list must be changeable: adding to it afterwards works.

    For example, Masala chai, Cutting chai with the special Kulhad chai gives Masala chai, Cutting chai, Kulhad chai; the menu still has 2 items, and adding Bun maska to the new list makes it 4.

  6. Task 6 · interview question

    Interview: Menu.java, predict which loops throw

    In Menu.java, make predictRemovals() return three strings, one per target in the comment's order (Cutting chai, then Masala chai, then Bun maska). Each run starts from a fresh list and runs the loop in the comment. When a run throws, the string is the simple name of the exception (for example IllegalStateException, without the package); when it finishes, the string is the list printed with toString (for example [Masala chai, Bun maska]).

    The check runs the same loops and compares your list with what Java really did.

What you will learn

  • ArrayList: a list that grows and shrinks, with add, addFirst, set, remove and indexOf.
  • Two removes: remove(int index) and remove(Object value), and which one Java picks for a List<Integer>.
  • Unmodifiable lists: List.of and List.copyOf, which refuse every change.
  • Removing while you loop: why a for-each loop breaks, and the iterator that does it safely.

More in Core Java

Build "Lists" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.