Homework: Standard Java Lists
-
Look up the online documentation for
java.util.List, a standard Java container component (in the style of ourQueue,Stack,Sequence, etc.). -
Carefully read the following sections of the documentation:
- the top-level description of the
Listcomponent at the start of the page, and - the detailed descriptions of the methods
add(E e),remove(int index),get(int index), andsize().
- the top-level description of the
-
Complete (and print with your homework) the following tracing table:
Statement Variable Values List<Integer> list = new SomeListImplementation<>(); list.add(7); list.add(-12); list.add(3); int x = list.size();
x = list.get(1);
x = list.remove(0);
x = list.remove(1);
x = list.size();
-
You may have observed that the
add(E e)andremove(int index)methods are marked as optional operations. Briefly discuss the benefits vs. pitfalls of this design decision. -
Consider this quote from the
java.util.Listdescription: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.