Dictionaries - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Dictionaries

Description:

Fig. 17-2 Two iterators that traverse a dictionary's keys and values in parallel. ... If not in dictionary, add it, initialize word count = 1 ... – PowerPoint PPT presentation

Number of Views:245
Avg rating:3.0/5.0
Slides: 14
Provided by: steve1789
Category:

less

Transcript and Presenter's Notes

Title: Dictionaries


1
Dictionaries
  • Chapter 17

2
Chapter Contents
  • Specifications for the ADT Dictionary
  • Java Interface
  • Iterators
  • Applications
  • Phone Book
  • Frequency of Words
  • Concordance of Words
  • Java Class Library Map Interface

3
Specifications for the ADT Dictionary
  • Contains entries that each have two parts
  • A key word or search key
  • A value associated with the key

Fig. 17-1 An English dictionary.
4
Specifications for the ADT Dictionary
  • Data
  • Pairs of objects (key, value)
  • Number of pairs in the collection
  • Operations
  • add
  • remove
  • getValue
  • contains
  • getKeyIterator
  • getValueIterator
  • isEmpty
  • isFull
  • getSize
  • clear

5
Iterators
  • Note that getKeyIterator and getValueIterator
    return iterators
  • Create iterators for a dictionary myTable
    withIterator keyIterator myTable.getKeyIterator
    ()Iterator valueIterator myTable.getValueItera
    tor()
  • An iteration of a dictionary's values
  • Corresponds to an iteration of the search keys

6
Iterators
Fig. 17-2 Two iterators that traverse a
dictionary's keys and values in parallel.
7
Using the ADT Dictionary
  • A directory of telephone numbers

Fig 17-3 A class diagram for a telephone
directory.
8
A Directory of Telephone Numbers
  • The beginnings of TelephoneDirectory

import java.io.import java.util.public
class TelephoneDirectory private DictionaryInt
erface phoneBook private static final String
DELIMITERS " \n\r\t" public
TelephoneDirectory() phoneBook new
SortedDictionary() . . .
9
A Directory of Telephone Numbers
  • Other methods may include

/ Task Reads a text file of names and
telephone numbers. _at_param dataFile a text file
that is open for input /public void
readFile(BufferedReader dataFile)
throws IOException public String getPhoneN
umber(String first, String last)
public String getPhoneNumber(Name personName)
/ Task Interacts with user /public String
getPhoneNumber()
10
The Frequency of Words
  • We seek an ADT that will enable us to count each
    occurrence of a word as it is read from a text
    file
  • To determine the frequency of a word
  • The word must be the search key
  • The data field will be of the class
    FrequencyCounter
  • Read a word from the file
  • If not in dictionary, add it, initialize word
    count 1
  • If it is in the dictionary, increment the word
    count

11
A Concordance of Words
  • Provide an index for finding a word in a file
  • Index gives line (or page) number
  • A word may appear multiple times
  • Appears once in index/concordance
  • Associated data is a list of line (page) numbers

12
Concordance...Example
  • Learning without thought is labor lost
  • thought without learning is perilous.
  • is 1 2
  • labor 1
  • learning 1 2
  • lost 1
  • perilous 2
  • thought 1 2
  • without 1 2

13
Java Class Library The Interface Map
  • A list of method signatures in Map
  • Note similarity to ADT Dictionary
  • Not String-specific!

public Object put(Object key, Object
value)public Object remove(Object key)public
Object get(Object key)public boolean
containsKey(Object key)public boolean
containsValue(Object value)public Set
keySet()public Collection values()public
boolean isEmpty()public int size()public void
clear()
Write a Comment
User Comments (0)
About PowerShow.com