187
本投影片(下稱教用資源)僅授權給採用教用資源相關之旗標書籍為教科書之授課老師(下稱老師)專用,老 師為教學使用之目的,得摘錄、編輯、重製教用資源(但使用量不得超過各該教用資源內容之 80% )以製作為 輔助教學之教學投影片,並於授課時搭配旗標書籍公開播放,但不得為網際網路公開傳輸之遠距教學、網路教 學等之使用;除此之外,老師不得再授權予任何第三人使用,並不得將依此授權所製作之教學投影片之相關著 作物移作他用。 著作權所有 © 旗標出版股份有限公司 集合與泛型 18

SCJP ch18

Embed Size (px)

Citation preview

  • 1. 18 80%

2. Collection Collections Arrays 3. java.util , (Collection) , , ArrayList LinkList TreeSet , , Object equals() hashcode() , 4. , Java 5 (Generic Type) , ( String ), , 5. 18-1 (Collection) , , , Java API java.util , , Collection ( ) , Java Collection Collections Framework 6. (Collection) Java Collections Framework, Collections Framework SCJP , 7. 18-1-1 Collections Framework ( ) , Collections Framework , , 8. Collections Framework Collections Framework , , 2 9. Collections Framework Collection , Collection , , , / Set SortedSet Set , Collection , Set / SortedSet , SortedSet , 10. Collections Framework List List , List , List , Queue List , , , , (First In First Out) , 11. Collections Framework Map SortedMap Map (key-value) , , Map , key ( ) SortedMap , Map , , java.util 12. 18-1-2 Collection , Collection , Set List , , Collection , , Collection 13. iterator() , , , Iterator iterator() , (int long double ) (Integer Long Double ) , 14. , Collection 15. , , , , , 16. Collections java.util , Collections Collection , , static , , Collections, , Collection , Map Collections List Collections static 17. 18-1-3 equals() hashcode() , , contains(Object o) o , Object equals() , , (Hash table) , HashSet HashMap ( Hash ) , Object hashcode() (Hash code), 18. equals() hashcode() Object equals() == , hashcode() ( ), , , , 19. equals() hashcode() 20. equals() hashcode() 21. equals() hashcode() , , , (id) 22. equals() hashcode() 23. equals() hashcode() equals() hashCode() , toString() Object 3 public, equals() Object ( Student ), Java (Integer Long...) String , Object , 24. Student ( ) int, , "RA001" ( RA ) , "SE23q0Xt9z45Ke81Puig" , , , , , 25. , , 3 1. , , 4502 ( , ) 2. ( equals() ), , , 2 , 4502 4502, 3. , , , , 4502 26. , , , equals() , , , , equals() , 27. hashCode() , , , , , , ( ) ( ), 28. 18-1-4 Set Set , Set , Set , Collection , add() addAll() Set Set Abs tractSet , HashSet LinkedHashSet TreeSet Set SortedSet , TreeSet SortedSet 29. Set Hash , , Object hashCode() 30. Set , 10 TreeSet , , , , , 31. , 5.0 JDK, , (Note) JDK 5.0 Generics ( ) , , , 32. 5.0 , , 33. Set HashSet LinkedHashSet TreeSet 34. HashSet HashSet , HashSet HashSet , add() , Collection , HashSet 35. HashSet HashSet , HashSet , , HashSet HashSet , , HashSet , 36. HashSet 37. HashSet 38. HashSet1. 15 HashSet words 39. HashSet 2. 17 24 while , , , 3. 18 21 if add() , add() false, (Set ) break 4. 27 words , , toString() 40. HashSet , HashSet , HashSet , LinkedHashSet 41. LinkedHashSet LinkedHashSet HashSet , , LinkedHashSet LinkedHashSet 42. LinkedHashSet 43. LinkedHashSet 44. LinkedHashSet , LinkedHashSet , , toArray() , , toArray() 45. TreeSet TreeSet 2 , TreeSet SortedSet , , 46. LinkedHashSet , int, first() last() Object int ( Object ) Integer , (Unboxing) int String , ( ) , , 18-1-10 47. LinkedHashSet 48. LinkedHashSet 1. 3 , 6 Arrays asList() List , ts 2. , < - < 0~9 < A~Z < a~z < , xxxSet() , , , , 49. LinkedHashSet 50. LinkedHashSet 51. LinkedHashSet 1. 8 10 TreeSet , 10 100 10 , 10 , add(new Integer(i*10)) add(i*10), int (Autoboxing) Integer 2. 14 50 16 clear() 3. 11 18 , 52. LinkedHashSet , , , xxxSet() , 53. headSet subSet tailSet (Backed Collection) , , , 54. 18-1-5 List List List , List , (index) List , Collection add() remove() , 55. List 56. List add() set() index element, set() add() , 57. List java.util List AbstractList , AbstractSquentialList ArrayList LinkedList , 58. ArrayList ArrayList , , , , , , , , ArrayList / , ArrayList 2 59. ArrayList ArrayList 60. ArrayList 61. ArrayList1. 8 ArrayList , 2. 12 13 , 3. 15 17 add() , 4. 21 22 Collection get() 62. ArrayList 1 , , add() , , add() , , , 63. ArrayList , , ArrayList , , Collection toArray() 64. LinkedList LinkedList List , LinkedList 65. LinkedList , LinkedList (Data Structure) (stack) (LIFO, Last In First Out) , ( ) , , 66. LinkedList , 67. LinkedList (queue) (FIFO) , , 68. LinkedList , , java.util LinkedList , , LinkedList , LinkedList 69. LinkedList 70. LinkedList 71. LinkedList 72. LinkedList 1. 15 LinkedList 2. 16 26 for 3. 17 18 addFirst() 4. 19 26 removeFirst() 73. LinkedList 5. 23 26 , NoSuchElementException , , , , LinkList List Queue , Queue offer() TIIP poll() peek() , 74. 18-1-6 Queue Queue (FIFO, First In Fisrt Out) , 6 , 3 , 3 75. Queue Queue , LinkList ( List Queue ) , PriorityQueue 76. PriorityQueue PriorityQueue (Priority) , , (FIFO) ( ), ( Comparotor ), 18-1-10 ( ) 77. PriorityQueue 78. 18-1-7 Map Map (key-value pair) , , key value , key , , , 79. Map , Map isEmpty() size() Collection , / 80. Map Map 81. Map SortedMap Map , SortedSet 82. Map Map/SortedMap 83. Map AbstractMap AbstractCollection toString() , System.out.println() Map AbstractMap 5 , HashMap TreeMap LinkedHashMap , HashMap Set HashSet, 84. Map TreeMap TreeSet , ( ) , , , headMap() subMap() tailMap() ( ) LinkedHashMap LinkedHashSet , HashMap 85. Map 86. Map 87. Map 1. 12 HashMap 2. 14 15 for cities HashMap 3. 20 21 4. 24 , 88. Vector Hashtable ArrayList HashMap , (Thread safe) , (Synchronized) Hashtable t ( ), HashMap , key value null, HashMap null key, null value SCJP , 89. 18-1-8 Iterator Iterator ( ) java.util , , Collection iterator() , Iterator , Iterator Map iterator() , 18-26 entrySet() Map , Iterator 90. Iterator Iterator 3 SubTree.java , Iterator 91. Iterator 92. Iterator 93. Iterator 1. 8 10 TreeSet 1 100 2. 14 Iterator i 3. 16 18 while() , 9 , 4. 20 , 9 94. For-Each , For-Each JDK 5.0 , For-Each For-Each , , , for For-Each 95. For-Each , For-Each 96. For-Each 97. For-Each 1. 8 JDK 5.0 , TreeSet 2. 15 For-Each , Integer i, IntTS For-Each , IntTS , i 98. 18-1-9 TreeSet TreeMap PriorityQueue , (Natural order) , String (A B, ), int , , , , Java 99. , Comparable compareTo() , , , Comparator 100. Comparable Comparable , CompareTo() , 101. Comparable 102. Comparable 10~11 Comparable compareTo() , public int, 0 , 0 ( ), 0 Comparable , Comparable, compareTo(Objet o) compareTo(Student o), (Student) o ( 18-2 ) 103. Comparable 2. compareTo() String Integer , compareTo() , 11 3. 17~20 3 104. Comparable , , , CompareTo() , TreeSet 5, 6L ClassCastException 6L , 6L (Long) compareTo(Long ) , (5, Integer) Long 105. Comparator Comparable , ( ) , , Comparator , Student 106. Comparator 107. Comparator 1. 13 17 , Comparator compare() , 2 , Comparable compareTo() Comparator , Comparator, compare() compareTo(Student o1, Student o2), (Student) o1 o2 ( 18-2 ) 108. Comparator 2. 23 28 TreeSet , comare() , , 109. 18-1-10 Collections Arrays Collections Arrays , ... Collections Collection , 110. Collections Arrays Collections , List , ArrayList Arrays , 111. Collections Arrays 112. Collections Arrays Book (Book.java), 113. Collections Arrays 114. Collections Arrays Book Object toString() equals() hashCode() , compareTo() BookByName 115. Collections Arrays 116. Collections Arrays 117. Collections Arrays 1. 4~9 , 11 List ArrayList , 2 2. 17~21 List , 23~27 List 118. Collections Arrays Collections.binarySearch() , List , comparator , binarySearch() comparator , ( 0 int) , , ( key List , ) x , (-x-1), 103 -2, (-(2)-1) = 1 119. 18-1-11 TreeSet TreeMap Java 6 , TreeSet TreeMap (Navigating) NavigatableSet NavigatableMap 120. Collections Arrays NavigatableSet 121. Collections Arrays NavigatableMap , Key Entry Java 5 Java 6 , 122. Collections Arrays 123. Collections Arrays 1. 8 9 Java 5 , headSet() 140 , last() 2. 12 Java 6 , lower() 124. Collections Arrays , NavigatableXxx (Xxx Set Map) headXxx() tailXxx() subXxx() , boolean , , boolean NavigatableXxx , boolean SortedXxx 125. 18-2 (Generic Types) , (Type Safe) , , String Integer , , , Java , ( Object ) 126. 18-2-1 ( ) , Java 5 (Generic Types) , "< >", , 127. < > , (Type parameter) (Parameterized type), , , , 128. , tm.get(1) String , , int , 129. , , newBook( ) ArrayList (Book Book.java, 8-1-10 ) 130. 131. 132. 18-2-2 , Java , 133. 134. 1. 5 6 , 10 newBook , b , 2. 14 , Book 135. 3. , Note , ( ) , -Xlint:unchecked , , 136. 18-2-3 Java , (Type erasure) , .class (Byte code) , 137. , , , newBook() 138. TreeSet , newBook() String , TreeSet ( ), 6 , Book 139. , 10 String , JVM , , , , ( ), Integer Object , String ( ArrayStoreException 140. 18-2-4 Java , , , TreeSet , TreeSet , Long 141. , , , 142. , , , Integer Object , , , 143. 144. , ArrayList TreeSet, 6 ClassCastException , 5 (Integer) Long 6L compareTo(Long) 145. 18-2-5 , , , TreeSet TreeSet... TreeSet , (Wildcard type) 146. 147. 1. 2 List sortList() 2. 7 11 15 ArrayList , 148. sortList() Number , 17 , String Number extends , , extends implements 149. , List List List , List< >, List List 150. super , , sortList() 151. , 3 cannot find symbol , aList add() , ArrayList , Integer , , , , , class T { void T(T T) { } ; } , ( 5 T ) 174. 18-A 18-B 175. 1. Given:What is the result? A. b,a,c,B. c,a,b,C. c,b,a,D. Compilation fails. E. An exception is thrown at runtime. 176. 2. Please place the correct Strings to the Output empty boxes. 177. 3. Which class implements the NavigatableMap ? A. HashMap. B. SortedMap. C. TreeMap. D. LinkedHashMap. E. NavigateMap. 178. 4. Given: 179. What is the result? A. 2 B. 3 C. 4 D. 16 E. Compilation fails. 180. 5. Given:What is the result? A. 3.14 50 100 B. 100 50 3.14 C. Compilation fails. D. An exception is thrown at line 4. E. An exception is thrown at line 5. F. An exception is thrown at line 7. 181. Place the correct Animals to the Outputs empty boxes. (Animals can be used more than once . ) 182. 7. Which two, inserted at line 1, will compile? (Choos two.)A. B. C. D. E. F.class MaxValue { class MaxValue { class MaxValue { class MaxValue { class MaxValue { class MaxValue