Homework: Standard Java Lists


  1. Look up the online documentation for java.util.List, a standard Java container component (in the style of our Queue, Stack, Sequence, etc.).

  2. Carefully read the following sections of the documentation:
    • the top-level description of the List component at the start of the page, and
    • the detailed descriptions of the methods add(E e), remove(int index), get(int index), and size().

  3. Complete (and print with your homework) the following tracing table:

    Statement Variable Values
    List<Integer> list = new SomeListImplementation<>();
    list =
    list.add(7);
    list =
    list.add(-12);
    list =
    list.add(3);
    list =
    int x = list.size();
    list =
    x =
    x = list.get(1);
    list =
    x =
    x = list.remove(0);
    list =
    x =
    x = list.remove(1);
    list =
    x =
    x = list.size();
    list =
    x =

  4. You may have observed that the add(E e) and remove(int index) methods are marked as optional operations. Briefly discuss the benefits vs. pitfalls of this design decision.

  5. Consider this quote from the java.util.List description:
    Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements.
    Briefly discuss the benefits vs. pitfalls of this design decision.