【正文】
ublic class SetDemo6 { public static void main(String args[]) { Set s = new TreeSet()。 (new Integer(37))。 while (()) (())。 the objects are not parable D:\java SetDemo6 Exception in thread main D:\ ? The exception is thrown because an attempt is made to order the elements of the set. A String object has no relationship to an Integer object, so the relative order of the two objects cannot be determined. 17 1. 對(duì)數(shù)組對(duì)象的操作 (Arrays) 2. 對(duì)象集合 (Set) 3. 對(duì)象列表 (List) 4. 對(duì)象映射 (Map) 5. 對(duì)對(duì)象數(shù)組的操作 (Collections) 6. 枚舉 (Enumeration)和迭代 (Iterator) 7. 小結(jié) 第八章 集合操作 18 ? 有序?qū)ο蟮募? ? A List is an ordered collection (sometimes called a sequence). ? Lists can contain duplicate elements. ? The user can access elements by their integer index (position), and search for elements in the list. ? 方法操作 ? 列表與元素 ? 列表之間 對(duì)象列表 (List) 19 ? 接口 (列表與元素 ) ? public void add(int index, Object element) Inserts the specified element at the specified position in this list. ? public Object remove(int index) Removes the element at the specified position in this list. ? public boolean contains(Object o) Returns true if this list contains the specified element. ? public int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or 1 if this list does not contain this element. 對(duì)象列表 (List) 20 ? 接口 (列表之間 ) ? public boolean addAll(Collection c) Appends all of the elements in the specified collection to the end of this list. ? public boolean removeAll(Collection c) Removes from this list all the elements that are contained in the specified collection. ? public boolean containsAll(Collection c) Returns true if this list contains all of the elements of the specified collection. ? public boolean retainAll(Collection c) Retains only the elements in this list that are contained in the specified collection. 對(duì)象列表 (List) 21 ? implements import .*。 for (int i = 1。 i++) (i + * + i + = + i * i)。 while (()) (())。 Object get(Object key)。 boolean containsKey(Object key)。 int size()。 // 批量操作 void putAll(Map t)。 // Collection Views public Set keySet()。 public Set entrySet()。 Object getValue()。 } ? 27 對(duì)象映射 (Map) import .*。 public static void main(String args[]) { Map m = new HashMap()。 i。 (args[i], (freq==null ? ONE:new Integer(() + 1)))。 (m)。 public class MapDemo2 { public static void main(String args[]) { Map m = new HashMap()。 (”Larry”, ”2345678”)。 Iterator iter = ().iterator()。 (() + ” ” + ())。 public class SortDemo { public static void main(String args[]) { List list = new ArrayList()。 (“DEF”)。 (list)。 while(()) (())。 iter = ()。 } } import .*。 String s2 = (String)o2。 } } class Collections ? public static void sort(List list) Sorts the specified list into ascending order, according to the natural ordering of its elements. class Collect