Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Fundamentals of Java Chapter 14: Introduction to Collections UNIT 4— LOOKING AHEAD CHAPTER 14— INTRODUCTION TO COLLECTIONS EXERCISE 14.3 1. public static int sum(List<Integer> numbers){ int total = 0; for (int number : numbers) total += number; return total; } 2. for (int i = 0; i < list.size(); i++) System.out.println(list.get(i)); 3. The running time for an array list is O(n), whereas the running time for a linked list is O(n2). An enhanced for loop, which uses an iterator, has a running time of O(n) for both types of lists: for (int element : list) System.out.println(element); 4. Iterator<Integer> iter = list.iterator(); while (iter.hasNext()){ iter.next(); iter.remove(); } EXERCISE 14.4 1. a. 30 25 44 55. b. 30 25 44. c. 30 25 2. boolean isPalindrome = true; Stack<char> stk = new Stack<char>(); for (char ch : source) stk.push(ch); int i = 0; while (! stk.empty()) if (source.chartAt(i) != stk.pop()){ isPalindrome = false; break; } else i++; 3. a. 138 b. 220 4. Scanner tokens = new Scanner(source); while (tokens.hasNext()){ 1 Fundamentals of Java Chapter 14: Introduction to Collections String token = tokens.next(); System.out.print(token); if (Character.isDigit(token.charAt(0))) System.out.println(" : integer"); else System.out.println(" : operator"); } EXERCISE 14.5 1. a. 30 25 44 55. b. 25 44 55. c. 25 44 55 13 2. while (! s.empty()) q.add(s.pop()); 3. 44 25 30 EXERCISE 14.6 1. while(! list.isEmpty()) s.add(list.remove(0)); 2. 33 67 100 44, in no particular order. 3. int sum = 0; for (int number : s) sum += number; System.out.println(sum); 4. public static Set<integer> union(Set<integer> s1, Set<integer> s2){ Set<Integer> result = new HashSet<Integer>(); for (int number : s1) result.add(number); for (int number : s2) result.add(number); return result; } EXERCISE 14.7 1. 2. Map<String, Integer> m = new HashMap<String, Integer>(); for (int i = 0; i < keys.size; i++) m.put(keys.get(i), values.get(i)); String highestName = ""; int highestGrade = Integer.MIN_VALUE; Set<String> keys = m.keySet(); for (String currentName : keys){ int currentGrade = m.get(currentName); if (currentGrade > highestGrade){ highestGrade = currentGrade; highestName = currentName; } 2 Fundamentals of Java Chapter 14: Introduction to Collections System.out.println(highestName); 3. int highestGrade = Integer.MIN_VALUE; Set<String> keys = m.keySet(); for (String currentName : keys){ int currentGrade = m.get(currentName); if (currentGrade > highestGrade) highestGrade = currentGrade; } Set<String> names = new HashSet<String>(); for (String currentName : keys){ int currentGrade = m.get(currentName); if (currentGrade == highestGrade) names.add(currentName); } for (String currentName : names){ System.out.println(currentName); REVIEW QUESTIONS Fill in the Blank 1. string and list 2. stack 3. queue 4. set 5. array list and linked list 6. array list 7. map 8. iterator 9. List 10. Collection 3