190 likes | 277 Views
A Quick Recursive Example. CMSC 150. Recall - StringList : A Recursive Class. public class StringList { // instance variables private boolean isEmpty ; private String thisString ; private StringList restOfStringList ; // constructors public StringList ( ) { … }
E N D
A Quick Recursive Example CMSC 150
Recall - StringList: A Recursive Class public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { … } public StringList(StringnewString, StringListaList) { … } public String toString() { … } public String getLineStartingWith(String prefix) { … } }
StringList: Constructors public class StringList { // instance variables private booleanisEmpty; private String thisString; private StringListrestOfStringList; // constructors public StringList() { isEmpty = true; thisString = ""; restOfStringList = null; } • public StringList(String newString, StringListaList) { • isEmpty = false; • thisString = newString; • restOfStringList = aList; • } }
Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); "" strings
Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); strings = new StringList( "firstAdded", strings ); "" strings "firstAdded"
Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); strings = new StringList( "firstAdded", strings ); strings = new StringList( "secondAdded", strings ); "" strings "secondAdded" "firstAdded"
Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); strings = new StringList( "firstAdded", strings ); strings = new StringList( "secondAdded", strings ); strings = new StringList( "thirdAdded", strings); "" strings "secondAdded" "firstAdded" "thirdAdded"
Creating the string version public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); }
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList "firstAdded " public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList "secondAdded firstAdded " public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList "thirdAdded secondAdded firstAdded " public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay
Building a StringList "thirdAdded secondAdded firstAdded " public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay thirdAdded secondAdded firstAdded