Chapter 7.4.2 – Association Lists | Introduction to Programming Using Java

Chapter 7.4.2 – Association Lists | Introduction to Programming Using Java

 

7.4.2 Association Lists

 

One particularly common application of searching is with association lists. The standard example of an association list is a dictionary. A dictionary associates definitions with words. Given a word, you can use the dictionary to look up its definition. We can think of the dictionary as being a list of pairs of the form (w,d), where w is a word and d is its definition. A general association list is a list of pairs (k,v), where k is some “key” value, and v is a value associated to that key.

In general, we want to assume that no two pairs in the list have the same key. There are two basic operations on association lists: Given a key, k, find the value v associated with k, if any. And given a key, k, and a value v, add the pair (k,v) to the association list (replacing the pair, if any, that had the same key value). The two operations are usually called get and put.

Association lists are very widely used in computer science. For example, a compiler has to keep track of the location in memory associated with each variable. It can do this with an association list in which each key is a variable name and the associated value is the address of that variable in memory.

Another example would be a mailing list, if we think of it as associating an address to each name on the list. As a related example, consider a phone directory that associates a phone number to each name. The items in the list could be objects belonging to the class:

 

Chapter 7.4.2 - Association Lists | Introduction to Programming Using Java

 

The data for a phone directory consists of an array of type PhoneEntry[ ] and an integer variable to keep track of how many entries are actually stored in the directory. The technique of “dynamic arrays” (Subsection 7.3.2) can be used in order to avoid putting an arbitrary limit on the number of entries that the phone directory can hold.

 

Using an ArrayList would be another possibility. A PhoneDirectory class should include instance methods that implement the “get” and “put” operations. Here is one possible simple definition of the class:

 

Chapter 7.4.2 - Association Lists | Introduction to Programming Using Java

Chapter 7.4.2 - Association Lists | Introduction to Programming Using Java

43 Chapter 7.4.2 - Association Lists | Introduction to Programming Using Java

The class defines a private instance method, find(), that uses linear search to find the position of a given name in the array of name/number pairs. The find() method is used both in the getNumber() method and in the putNumber() method. Note in particular that putNumber(name,number) has to check whether the name is in the phone directory. If so, it just changes the number in the existing entry; if not, it has to create a new phone entry and add it to the array.

This class could use a lot of improvement. For one thing, it would be nice to use binary search instead of simple linear search in the getNumber method. However, we could only do that if the list of PhoneEntries were sorted into alphabetical order according to name. In fact, it’s really not all that hard to keep the list of entries in sorted order, as you’ll see in the next subsection.

 

 

Read More…

1 thought on “Chapter 7.4.2 – Association Lists | Introduction to Programming Using Java”

Leave a Comment