Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HW5;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- /**
- A table for lookups and reverse lookups
- */
- public class LookupTable
- {
- private ArrayList<Item> byName;
- private ArrayList<Item> byNumber;
- /**
- Constructs a LookupTable object.
- */
- public LookupTable()
- {
- byName = new ArrayList<Item>();
- byNumber = new ArrayList<Item>();
- }
- /**
- Reads name and number pairs from the Scanner
- and adds them to the byName and byNumber array lists.
- @param in the scanner for reading the input
- */
- public void read(Scanner in)
- {
- while (in.hasNextLine())
- {
- // Read a line containing the name
- String name = new String(in.nextLine());
- // Read a line containing the number
- String number = new String(in.nextLine());
- // Store the name and number in the byName array list
- Item Name = new Item(name, number);
- byName.add(Name);
- // Store the number and name in the byNumber array list
- Item Number = new Item(number, name);
- byNumber.add(Number);
- }
- // Sort the byName Items so we can binary search
- Collections.sort(byName);
- // Sort the byNumber Items so we can binary search
- Collections.sort(byNumber);
- }
- /**
- Looks up an item in the table.
- @param k the key to find
- @return the value with the given key, or null if no
- such item was found.
- */
- public String lookup(String k)
- {
- // Use the Collections.binarySearch() method to find the
- // position of the matching name in the byName array list.
- // Return null if position is less than 0 (not found).
- // Otherwise, return the number for the found name.
- }
- /**
- Looks up an item in the table.
- @param v the value to find
- @return the key with the given value, or null if no
- such item was found.
- */
- public String reverseLookup(String v)
- {
- // Use the Collections.binarySearch() method to find the
- // position of the matching number in the byNumber array list.
- // Return null if position is less than 0 (not found).
- // Otherwise, return the name for the found number.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement