13.ppt

advertisement
CS110 Lecture 13
Thursday, March 11, 2004
• Announcements
– hw5 due tonight
– Spring break next week
– hw6 due two weeks from tonight
• Agenda
–
–
–
–
questions
TreeMap
hw6
stub programming
Lecture 13
1
Maps
• arrays and ArrayLists locate entries by index
– index is an integer position, starting at 0
• A Map locates entries by key
– key is often a String (think dictionary, phone book)
• A map stores key-value pairs
– key: “Java”
value:
“a modern OO language”
PhoneNumber
areaCode: 617
exchange: 287
number: 5000
– key: “UMass” value:
Lecture 13
2
TreeMapDemo.java
• Class Integer to wrap primitive int
• Iterator practice
• Useful toString (for debugging)
• Remember to cast
• Same value stored twice
• Keys that aren’t Strings
Lecture 13
3
Getting an Iterator
Set keys = map.keySet();
Iterator keysIterator =
keys.iterator();
•
•
•
•
ask the map for its keySet
ask the keySet to give you an Iterator
keysIterator is like a list of the keys in the map
You can infer from this code that
– Set and Iterator are classes in the Java API
– keySet is a method in class TreeMap; it returns a Set
– iterator is a method in class Set; it returns an Iterator
Lecture 13
4
Using an Iterator
while ( keysIterator.hasNext() ) {
String key =
(String)keysIterator.next();
terminal.println( " … " +
(Integer)map.get( key)) );
}
• hasNext() returns false when at end of list
• next() returns a reference to the next Object in the list
• Iterator next method, like TreeMap get method, returns
just an Object. Need two casts.
Lecture 13
5
Class Lookup
• Dictionary has no unit test
• Class Lookup is a client for Dictionary
(and tests it thoroughly)
• All of Lookup is static
> java Lookup <word> <word> … all
• Lookup.java sends toString messages to a
Definition object (line 53) and to a
Dictionary object (line 103)
Lecture 13
6
Dictionary boxes and arrows
Dictionary
entries:
entries:
TreeMap
TreeMap
Object Object
"a geometric object in a plane"
String
"shape"
Definition
String
definition:
entries:
Object Object
•••
"quadrilateral"
Definition
String
definition:
entries:
"a shape with four sides"
Lecture 13
7
Looping on a TreeMap
• To print the whole Dictionary, Lookup sends a
toString message, invoking Dictionary
toString method (line 70)
• Subtle, since there’s no index to loop with
• Uses an Iterator object Java tool custom designed for looping
• Iterator API has just two methods:
– boolean hasNext()
– Object next()
Lecture 13
8
Building a multiline String
while ( wordIterator.hasNext() ) {
word = (String)wordIterator.next();
definition = this.getEntry( word );
str += word + ":\n" +
definition.toString() + "\n";
}
• use the key to look up a Definition
• send the Definition a toString message
• add two lines to the String str we are building to
represent the whole Dictionary
Lecture 13
9
TreeMap summary
•
•
•
•
declaration: TreeMap mapName;
creation: new TreeMap( );
put:
mapName.put(Object key, Object obj)
get:
(Type)mapName.get(Object key)
cast to proper Type
• length:
mapName.size( )
• looping:
get Set of keys from the map, then get an
Iterator from the set
mapName.keySet( ).iterator( )
Lecture 13
10
Duplicates?
• The same value may appear more than once in a
collection (array, ArrayList or Map)
– my wife and I have the same phone number
– “field” and “instance variable” have the same definition
– in an array, foo[3] may == foo[7]
• In a Map, keys are unique (like index in an array)
• If you want to arrange for one person to have more
than one phone number or one word to have more
than one definition you need to work harder …
Lecture 13
11
Collections of collections
• Dictionary might map a word to an
ArrayList of Definition instances
• Registrar’s database maps a student ID to a
StudentRecord object that contains a Map
of courses taken (key course name, value an
object storing the grade)
• Screen maintains a private field that’s an
array of arrays of char:
private char[][] pixels;
Lecture 13
12
Figure 4.5
Object structure of a 2x3 Screen
Screen
int
width:
2
0: ‘ ‘
1: ‘ ‘
2: ‘ 5‘
char[][]
int
height:
char[]
0:
3
1:
char[][]
pixels:
char[]
0: ‘ ‘
1: ‘ ‘
2: ‘ 5‘
Lecture 13
13
hw6
• Due Thursday after Spring break
• TreeMap practice
– class Directory
– little Bank, using a TreeMap
Lecture 13
14
Directory
• Model windows folder
• Contains TextFiles
(not other Folders - wait for Chapter 5)
• API: create, add file to, get file from, get
size, get owner, get create/mod date
• Design: Directory object has a TreeMap
field storing TextFile objects keyed by
String filename
• You write this for homework
Lecture 13
15
Stub programming
• Start with all javadoc comments and
method declarations, empty method bodies
• Fill in code a method at a time
(perhaps even a line at a time)
• Compile and test as you go along!
Lecture 13
16
Download