【文章內(nèi)容簡介】
to an Integer object, so the relative order of the two objects cannot be determined. 17 1. 對數(shù)組對象的操作 (Arrays) 2. 對象集合 (Set) 3. 對象列表 (List) 4. 對象映射 (Map) 5. 對對象數(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. ? 方法操作 ? 列表與元素 ? 列表之間 對象列表 (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. 對象列表 (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. 對象列表 (List) 21 ? implements import .*。 public class ListDemo1 { public static void main(String args[]) { List list = new ArrayList()。 for (int i = 1。 i = 10。 i++) (i + * + i + = + i * i)。 Iterator iter = ()。 while (()) (())。 } } D:\java ListDemo1 1 * 1 = 1 2 * 2 = 4 3 * 3 = 9 4 * 4 = 16 5 * 5 = 25 6 * 6 = 36 7 * 7 = 49 8 * 8 = 64 9 * 9 = 81 10 * 10 = 100 D:\ 對象列表 (List) 22 對象列表 (List) (interface) . AbstractSequentialList (class) (class) (class) (class) ArrayList: List implemented via an array LinkedList: List implemented as a linked structure 23 1. 對數(shù)組對象的操作 (Arrays) 2. 對象集合 (Set) 3. 對象列表 (List) 4. 對象映射 (Map) 5. 對對象數(shù)組的操作 (Collections) 6. 枚舉 (Enumeration)和迭代 (Iterator) 7. 小結(jié) 第八章 集合操作 24 ? A Map is an object that maps keys to values ? A map cannot contain duplicate keys: Each key can map to at most one value. 對象映射 (Map) Key 1 (鍵 ) Value 1 (值 ) Key 2 (鍵 ) Value 2 (值 ) Key n (鍵 ) Value n (值 ) Map Entry 1 Entry 2 Entry n 25 對象映射 (Map) 對象列表 (List) (interface) (class) (class) HashMap: Map implemented via a hash table TreeMap: SortedMap implemented as a tree (class) 26 對象映射 (Map) public interface Map { //基本操作 Object put(Object key, Object value)。 Object get(Object key)。 Object remove(Object key)。 boolean containsKey(Object key)。 boolean containsValue(Object value)。 int size()。 boolean isEmpty()。 // 批量操作 void putAll(Map t)。 void clear()。 // Coll