CS111 StringListOps Contract

The StringListOps class contains some commonly used auxiliary class methods that manipulate string lists. All of these can easily be written in terms of the class methods from the StringList contract; they are provided here for convenience. Also for convenience, the StringListOps class also supplies all of the StringList class methods.

By default, a StringListOps class method is invoked via StringListOps.methodName(args), but this can be abbreviated to SL.methodName(args) by including the following "magical" declaration in the body of a class:

public static StringListOps SL; // declare but never initialize this variable.

Because StringListOps includes both core StringList methods and auxiliary methods, the above declaration enables using the prefix SL. to access all of these class methods.

Class Methods

public static StringList append (StringList L1, StringList L2)
Returns a string list whose length is the sum of the lengths of L1 and L2 and whose elements are all the elements of L1, in order, followed by all the elements of L2, in order.

public static boolean equals (StringList L1, StringList L2)
Returns true if L1 and L2 have the same length and the same elements in the same order (sameness of elements is determined by the equals instance method of the String class); otherwise returns false. Note that the equals method behaves differently than == on lists, which determines if two list nodes were created by the same call to prepend. For instance, if L is the list [the,dog,barks], then L.equals(prepend("the",tail(L))) is true, but L == prepend("the",tail(L)) is false.

public static boolean isMember (String s, StringList L)
Returns true if s is an element of the list L and false otherwise. Equality of strings is determined by the equals instance method of the String class.

public static int length (StringList L)
Returns the length of L -- i.e., the number of non-empty list nodes in L.

public static StringList postpend (IntList L, String s)
Returns a list whose length is one more than the length of L and whose elements are all the elements of L, in order, followed by n.

public static StringList reverse (StringList L)
Returns a list whose length is the same as that as L and whose elements are all the elements of L in reverse order.

For convenience, the StringListOps class also provides all the class methods of the StringList class:
public static StringList empty()
public static StringList prepend (String s, IntList L)
public static boolean isEmpty (StringList L)
public static String head (StringList L)
public static StringList tail (StringList L)
public static String toString (StringList L)
public static StringList fromString (String s)