1 / 19

A Quick Recursive Example

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 ( ) { … }

tamar
Télécharger la présentation

A Quick Recursive Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Quick Recursive Example CMSC 150

  2. 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) { … } }

  3. 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; • } }

  4. Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); "" strings

  5. Building a StringList In a method somewhere (maybe main ()) StringList strings = new StringList(); strings = new StringList( "firstAdded", strings ); "" strings "firstAdded"

  6. 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"

  7. 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"

  8. Creating the string version public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); }

  9. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  10. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  11. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  12. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  13. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  14. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  15. Building a StringList public String toString( ) { if (isEmpty) return ""; else return thisString + "\n" + restOfStringList.toString(); } "" strings "secondAdded" "firstAdded" "thirdAdded" String toDisplay = strings.toString( ); toDisplay

  16. 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

  17. 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

  18. 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

  19. 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

More Related