160 likes | 167 Vues
Choose the appropriate collection type for different scenarios such as finding the largest number, accessing elements using an index, spell checking, retrieving values in a specific order, looking up definitions, counting letter frequencies, finding word occurrences, managing printer jobs, and evaluating arithmetic expressions.
E N D
Exercise 1.a. Page 246 You want a collection that always returns the largest number. Which would you choose: • Queue • Priority Queue • Stack • List
Exercise 1.b. Page 246 You want a collection that provides access to an element using an index. • Queue • Set • List • Map
Exercise 1.c. Page 246 You want to input a word from a file and determine if it is in a collection of correctly spelled words. • LinkedList • TreeSet • TreeMap • ArrayList
Exercise 1.d. Page 246 The next value out is the last value that was put in. • Queue • Stack • List • Set
Exercise 1.e. Page 246 To get a value ¾ of the way through the collection, move through the proceeding values. • Stack • LinkedList • HashSet • ArrayList
Exercise 1.f. Page 246 The first value put in is the next value out. • Queue • Map • Priority Queue • Stack
Exercise 1.g. Page 246 Input a word, look it up in a collection, and output the definition of the word. • Queue • Set • Map • List
Another problem You want to count the frequency of the letters in a text passage and print them out in alphabetic order. • HashSet or TreeSet • Stack • Queue • Priority Queue • List • HashMap • TreeMap
Another problem You want to count the frequency of the letters in a text passage and print them out from most frequent to least frequent. • HashSet or TreeSet • Stack • Queue • Priority Queue • List • HashMap • TreeMap
Another problem You want to find all line numbers where a word appears in a text passage and print them out in order. • HashSet or TreeSet • Stack • Queue • Priority Queue • List • HashMap • TreeMap
Another problem A structure to hold jobs at a printer that will be handled on a first come, first served basis. • HashSet or TreeSet • Stack • Queue • Priority Queue • List • HashMap • TreeMap
Another problem You want to evaluate an arithmetic expression containing +, -, *, / in correct algebraic order (this is, 2+3*4 = 14) • HashSet or TreeSet • Stack • Queue • Priority Queue • List • HashMap • TreeMap
Implementing Set Operations • Use a bag to return the union of two sets passed in as parameters • Make your method generic
Implementing Set Operations • Use a bag to return the intersection of two sets passed in as parameters • Make your method generic
Implementing Set Operations • Use a bag to return the set difference of two sets passed in as parameters • Make your method generic
Implementing Set Operations • Write a test program to see if union, intersection, and set difference work properly