last updated 3/19/07
Many list operations require us to compare the values of objects. Examples include
We need an equality test (==)and relational tests (<, >, <=, >=)
Recall the equality comparison (==) operator checks if the variable contents are equal. If the variables are references to objects, are they pointing to the same object? e.g. are the addresses equal?
Since equals(), a boolean method, is exported from the Object class it can be used with objects of any Java class. For example, If c1 and c2 are objects of the class Circle, then we can compare them using c1.equals(c2)
As defined in the Object superclass, equals acts much the same as the comparison operator. By default, it returns true if and only if the two variables reference the same object. However, we can redefine (override) the equals method to fit the goals of the class.
A reasonable definition for equality of Circle objects is that they are equal if they have equal radii. We could alternatively define it according to position, or both.
To realize this equal radii approach we define the equals method of our Circle class to use the radius attribute:
public boolean equals(Circle circle)
// Precondition: circle != null
//
// Returns true if the circles have the same radius,
// otherwise returns false.
{
return (this.radius == circle.radius);
}
To support a sorted list we need to be able to tell when one object is less than, equal to, or greater than another object.
The Java library provides an interface, called Comparable, which can be used to ensure that a class provides this functionality.
The Comparable Interface consists of exactly one abstract method:
public int compareTo(Object o); // Returns a negative integer, zero, or a positive // integer as this object is less than, equal to, // or greater than the specified object.
The compareTo method returns an integer value that indicates the relative "size" relationship between the object upon which the method is invoked and the object passed to the method as an argument.
Objects of a class that implements the Comparable interface are called Comparable objects.
To ensure that all elements placed on our sorted list support the compareTo operation, we require them to be Comparable objects.
public int compareTo(Object o)
// Precondition: o != null
//
// Returns a negative integer, zero, or a positive integer as this object
// is less than, equal to, or greater than the parameter object.
{
if (this.radius < ((Circle)o).radius)
return -1;
else if (this.radius == ((Circle)o).radius)
return 0;
else
return 1;
}
Note that the equals method and the compareTo method of our Circle class are compatible with each other.
To simplify discussion we sometimes refer to the attribute, or combination of attributes, of an object used by the compareTo method to determine the logical order of a collection as the key of the collection.
Linear relationship: Each element except the first has a unique predecessor, and each element except the last has a unique successor.
Length or Size: The number of items in a list; the length can vary over time.
Unsorted list: A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationships.
Sorted list: A list that is sorted by the value in the key; there is a semantic relationship among the keys of the items in the list. Unless otherwise stated we'll assume ascending
Indexed list: A list in which each element has an index value associated with it
Key The attribute(s) that is(are) used to determine the logical order of the list. A key is a subset of attributes that uniquely determine the element of the collection.
Define a small, complete, but useful, set of operations for use with our lists.
Capture the formal specifications of our List ADT using a hierarchy of the Java interface construct. All the common list method descriptions are together into a single interface, called ListInterface.
We extend the ListInterface with three separate interfaces, one for each of our list variations.
The intent is that classes should implement one of these latter three interfaces, and therefore by extension, implement the ListInterface.
All three of our list variations provide the following operations:
int size(): Returns the number of elements on the list.boolean contains(Object): Passed an Object argument and returns a boolean indicating whether the list contains an equivalent element.
boolean remove(Object): Passed an Object argument and, if an equivalent element exists on the list, removes one instance of that element. Returns a boolean value indicating whether an object was actually removed.
Object get(Object): Passed an Object argument, it returns an equivalent object if one exists on the list. If not, returns null.
String toString(): Returns a nicely formatted string representing the list.
void reset(): Initializes the list for iteration. Sets the current position (the position of the next element to be processed) to the first element on the list.
Object getNext(): Returns the next element, and updates the current position.
The Unsorted List also supports the operation
The Sorted List also supports the operation
The Indexed List also supports the operations
Each method that accepts an index as an argument throws an exception if the index is invalid.

Code Section UnsortedListInterface list1 = new ArrayUnsortedList(); SortedListInterface list2 = new ArraySortedList(); IndexedListInterface list3 = new ArrayIndexedList(); System.out.print("Unsorted "); |
Output Unsorted List: Sorted List: Indexed List: |
The List interface (ListInterface.java)
The Unsorted List interface (UnsortedListInterface.java)
The Array Unsorted List class (ArrayUnsortedList.java)
The Sorted List interface (SortedListInterface.java)
The Array Sorted List class (ArraySortedList.java)