Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Peek at Lab 16Peek at Lab 16 Side effects and arraysSide effects and.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Peek at Lab 16Peek at Lab 16 Side effects and arraysSide effects and."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Peek at Lab 16Peek at Lab 16 Side effects and arraysSide effects and arrays Arrays of ‘references’ like StringsArrays of ‘references’ like Strings Sentinels and too-large arraysSentinels and too-large arrays SearchSearch Multiple dimensionsMultiple dimensions

2 March 2005 2R. Smith - University of St Thomas - Minnesota Lab 16 A “phone number lookup” procedureA “phone number lookup” procedure –Take a file of names and phone numbers –Read it into a too-large array, mark the end –Go into a loop looking up names/numbers Methods to writeMethods to write –Collect a file name, return a Scanner object –Read the file, create and fill in the array –Search for an entry matching what the user types –Main method Sets up the arraySets up the array Loops looking for requested namesLoops looking for requested names How do we ‘end it’? Pick a wayHow do we ‘end it’? Pick a way

3 March 2005 3R. Smith - University of St Thomas - Minnesota Side effects and arrays Why can’t variables in one method see those in another method?Why can’t variables in one method see those in another method? What’s the benefit of that?What’s the benefit of that? What are the problems with that?What are the problems with that? What if we change an array inside a method?What if we change an array inside a method?

4 March 2005 4R. Smith - University of St Thomas - Minnesota Review of Common Errors Chapter 4, page 227Chapter 4, page 227

5 March 2005 5R. Smith - University of St Thomas - Minnesota Arrays of ‘references’ or Strings A ‘String’ variable is a referenceA ‘String’ variable is a reference –It’s the address of a sequence of characters You can have arrays of references, just like you can have arrays of primitive typesYou can have arrays of references, just like you can have arrays of primitive types How can we compare two “reference” elements?How can we compare two “reference” elements? –Rainfall –Temperature –Strings

6 March 2005 6R. Smith - University of St Thomas - Minnesota Sentinels and too-large arrays What if we don’t know how big the array should be?What if we don’t know how big the array should be? How do we mark the “real” end of the array, if there are extra elements?How do we mark the “real” end of the array, if there are extra elements?

7 March 2005 7R. Smith - University of St Thomas - Minnesota Matching Strings Total match?Total match? Ignore case:Ignore case: –str1.toLowerCase().equals(str2.toLowerCase()) –What about toUpperCase()? Match the starting characters:Match the starting characters: –Str1.startsWith(startString) True if Str1 starts with startStringTrue if Str1 starts with startString How would we match parts of a person’s name?How would we match parts of a person’s name?

8 March 2005 8R. Smith - University of St Thomas - Minnesota Sequential Search Like looking for min/maxLike looking for min/max –Try to match a 3 rd value instead of min/max Iterate through and return the resultIterate through and return the result –Index –Actual value –Choice depends on what you’re doing Does it matter which loop you use?Does it matter which loop you use?

9 March 2005 9R. Smith - University of St Thomas - Minnesota Review of Common Errors Chapter 5, page 274Chapter 5, page 274

10 March 2005 10R. Smith - University of St Thomas - Minnesota Searching an array of strings What variables do we need?What variables do we need? What will we return?What will we return? What kind of loop should we use?What kind of loop should we use? How will we match text against an array element?How will we match text against an array element?

11 March 2005 11R. Smith - University of St Thomas - Minnesota Two dimensional array Two brackets instead of oneTwo brackets instead of one –Rows and columns –Array of 1 dimensional arrays CreatingCreating –New with 2 dimensions –Initializers with nested rows/columns No real limit to dimensionsNo real limit to dimensions

12 March 2005 12R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Peek at Lab 16Peek at Lab 16 Side effects and arraysSide effects and."

Similar presentations


Ads by Google