|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectgeneseo.cs.sc.List
Represents lists of arbitrary objects. This class reflects a recursive definition of "list", specifically a definition that says that a list is either
This class was created to support the text Algorithms & Data Structures: The Science of Computing by Doug Baldwin and Greg Scragg. All references herein to "the text" refer to that book. This class is the one described in Chapter 11, with a few extensions and with some methods completed that were "left to an exercise" in the text.
| Constructor Summary | |
List()
Initialize a list to be empty. |
|
List(java.lang.Object newItem)
Initialize a List to contain one element. |
|
| Method Summary | |
void |
addItem(java.lang.Object newItem)
Adds an object to a list. |
void |
concat(List extraList)
Concatenates two lists. |
List |
copy()
Create a duplicate of a list. |
void |
delete(java.lang.Object value)
Remove an object from a list. |
boolean |
find(java.lang.Object target)
Search a list for an object. |
java.lang.Object |
getAndRemove()
Removes a list's first item and returns it. |
java.lang.Object |
getFirst()
Extracts the head of a list. |
List |
getRest()
Extracts the tail of a list. |
boolean |
isEmpty()
Determines whether a list is empty or not. |
List |
makeNewList()
Create a new list that is an instance of the same class as this list. |
void |
printList()
Print the elements in a list from first to last. |
void |
printListForward()
Print the elements in a list from last to first. |
void |
removeItem()
Removes a list's head. |
void |
restore(java.lang.String fileName)
Restore a list from a file. |
void |
save(java.lang.String fileName)
Write a List to a file, replacing any previous data in that file. |
protected void |
setFirstItem(java.lang.Object value)
Replaces the head of a list with a new object. |
protected void |
setRest(List newTail)
Replaces the tail of a list with a new list. |
java.lang.String |
toString()
Generate a string representation of a list. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public List()
List things = new List();
public List(java.lang.Object newItem)
List words = new List( "lion" );
newItem - The single item the List will initially contain.| Method Detail |
public List makeNewList()
List. Every subclass of List
must therefore provide a method for handling this message that returns
a new instance of that subclass. Clients are unlikely to ever invoke
this method directly, but it is essential to the correct functioning of
other methods defined for lists.
makeNewList messagepublic java.lang.Object getFirst()
Object obj = myList.getFirst();
public List getRest()
List theRest = myList.getRest();
public boolean isEmpty()
if ( myList.isEmpty() ) ...
public void removeItem()
myList.removeItem();
public java.lang.Object getAndRemove()
Object oldHead = myList.getAndRemove();
public void addItem(java.lang.Object newItem)
words.addItem( "wombat" );
newItem - The object to be added to the list.protected void setFirstItem(java.lang.Object value)
words.setFirstItem( "aardvark" );
value - The object that will become the new head of the list.protected void setRest(List newTail)
myList.setRest( new List() );
newTail - The new tail for the list.public void concat(List extraList)
concat message.
For example
myList.concat( new List("more") );
extraList - The list to be added to the end of the current list.public void printList()
myList.printList();
public void printListForward()
myList.printListForward();
public boolean find(java.lang.Object target)
equals to compare
objects. For example
if ( words.find("capybara") ) ...
target - The object to look for in the list.
public void delete(java.lang.Object value)
equals
to compare the object to elements of the list. For example
words.delete("monkey");
value - The object to remove from the list.public List copy()
List savedList = myList.copy();
public void save(java.lang.String fileName)
myList.save( "ListFile" );
fileName - The name of the file to write the list to.public void restore(java.lang.String fileName)
myList.restore( "ListFile" );
fileName - The name of the file to restore the list from.public java.lang.String toString()
String text = myList.toString();
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||