The ObjectList class describes immutable linked lists of objects.
An immutable object list is defined recursively as either
Object lists are immutable in the sense that the head or tail of a list node
cannot be changed after the list node has been created (via the prepend() method).
There are no public constructor methods for immutable object lists, and
only one public instance method (toString()).
Instead, object lists are created and manipulated via the class methods
specified below.
By default, an ObjectList class method is invoked via
ObjectList.methodName(args), but this can
be abbreviated to OL.methodName(args)
by including the following "magical" declaration in the body
of a class:
public static ObjectList OL; // declare but never initialize this variable.
Class Methods
public static ObjectList empty()
Returns an empty object list.
public static ObjectList prepend (Object x, ObjectList L)
Returns a new object list node whose head isxand whose tail isL. From another perspective, returns a list whose length is one more than the length ofLand whose elements arexfollowed by the elements ofL, in order.
public static boolean isEmpty (ObjectList L)
ReturnstrueifLis an empty object list andfalseifLis an object list node.
public static Object head (ObjectList L)
Returns the object that is the head component of the object list nodeL. Signals an exception ifLis empty.
public static ObjectList tail (ObjectList L)
Returns the object list that is the tail component of the object list nodeL. Signals an exception ifLis empty.
public static String toString (ObjectList L)
Returns a string representation of the object listLin which the string representations of the list elements are separated by commas and delimited by square brackets. For example, ifL1is a list containing the sequence of strings"Sam","I","am", thentoString(L1)returns"(Sam,I,am)".
public static ObjectList fromString (String s)
Ifsis the printed representation of an object list (i.e., comma separated elements delimited by parentheses), returns anObjectListcontaining the strings of each element. For example,fromString("(Sam,I,am)")returns a 3-element object list with elements"Sam","I", and"am". Ifsis not the printed representation of an object list, aRuntimeExceptionis thrown.