The ObjectMList class describes mutable linked lists of objects.
An mutable object list is defined recursively as either
Mutable object lists are mutable in the sense that the head or tail of a list node
can can be changed (via setHead and setTail)
after the list node has been created.
There are no public constructor methods for object lists, and
only two public instance methods (equals() and toString()).
Instead, object lists are created and manipulated via the class methods
specified below.
By default, an ObjectMList class method is invoked via
ObjectMList.methodName(args), but this can
be abbreviated to OML.methodName(args)
by including the following "magical" declaration in the body
of a class:
public static ObjectMList OML; // declare but never initialize this variable.
Class Methods
public static ObjectMList empty()
Returns an empty mutable object list.
public static ObjectMList prepend (Object x, ObjectMList L)
Returns a new mutable object list node whose head isxand whose tail isL. From another perspective, returns a mutable list whose length is one more than the length ofLand whose elements arexfollowed by the elements ofL, in order.
public static boolean isEmpty (ObjectMList L)
ReturnstrueifLis an empty mutable object list andfalseifLis a mutable object list node.
public static Object head (ObjectMList L)
Returns the object that is the head component of the mutable object list nodeL. Throws an exception ifLis empty.
public static ObjectMList tail (ObjectMList L)
Returns the mutable object list that is the tail component of the object list nodeL. Throws an exception ifLis empty.
public static void setHead (ObjectMList L, Object x)
ModifiesLso thatxbecomes its head. Throws an exception ifLis empty.
public static void setTail (ObjectMList L, ObjectMList t)
ModifiesLso thattbecomes its tail. Throws an exception ifLis empty.
public static boolean equals (ObjectMList L1, ObjectMList L2)
ReturnstrueifL1andL2have the same length and have elements that are correspondingly equal via theequals()instance method. Otherwise returns false.
public static String toString (ObjectMList L)
Returns a string representation of the mutable 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 ObjectMList fromString (String s)
Ifsis the printed representation of an object list (i.e., comma separated elements delimited by square brackets), returns anObjectMListcontaining 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.
Instance Methods
public String toString ()
Returns a string representation of this mutable object list that is equivalent totoString(this).
public boolean equals (Object L)
Returns a boolean that is equivalent toequals(this,L)ifLis mutable object list andfalseotherwise.