版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、1對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作2java.util.Arrays類This class contains various methods for manipulating arrays排序(sorting)搜索(searching)填充(filling)對數(shù)組對象的操作(Arrays)3全部是靜態(tài)方法public static int binarySearch(byte a, byte key) Searches th
2、e specified array of bytes for the specified value using the binary search algorithm.public static boolean equals(byte a, byte a2) Returns true if the two specified arrays of bytes are equal to one another.public static void fill(byte a, byte val) Assigns the specified byte value to each element of
3、the specified array of bytes. public static void fill(byte a, int fromIndex, int toIndex, byte val) Assigns the specified byte value to each element of the specified range of the specified array of bytes.public static void sort(byte a) Sorts the specified array of bytes into ascending numerical orde
4、r.對數(shù)組對象的操作(Arrays)boolean, char, byte, short, int, long, double, floatObject4對數(shù)組對象的操作(Arrays)import java.util.Arrays;public class ArrayDemo1 public static void main(String args) int a1 = new int10; int a2 = new int10; Arrays.fill(a1, 47); Arrays.fill(a2, 47); System.out.println(Arrays.equals(a1, a2)
5、; a23=11; a22=9; System.out.println(Arrays.equals(a1, a2); Arrays.sort(a2); System.out.println(Arrays.binarySearch(a2, 11); 5對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作6數(shù)學(xué)上“集合”概念的抽象,無序A Set is a collection that cannot contain duplicate elem
6、ents. 集合與元素集合之間無序集合和有序集合對象集合(Set)7java.util.Set 接口 (集合與元素)public boolean add(Object o) Adds the specified element to this set if it is not already present.public boolean remove(Object o) Removes the specified element from this set if it is present.public boolean contains(Object o) Returns true if th
7、is set contains the specified element.public int size() Returns the number of elements in this set.對象集合(Set)8java.util.HashSet implement java.util.Set無序集合對象集合(Set)import java.util.*;public class SetDemo1 public static void main(String args) Set s = new HashSet(); for (int i=0; iargs.length; i+) if (
8、!s.add(argsi) System.out.println(Duplicate: +argsi); System.out.println(s.size()+ distinct words: +s); D:java FindDups1 i came i saw i leftDuplicate: iDuplicate: i4 distinct words: came, left, saw, iD:9java.util.Set接口 (集合之間)public boolean containsAll(Collection c) Returns true if this set contains a
9、ll of the elements of the specified collection.public boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if theyre not already present.public boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified colle
10、ction.public boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection.對象集合(Set)10java.util.HashSet implement java.util.Set對象集合(Set)import java.util.*;public class SetDemo2 public static void main(String args) Set uniques = new HashSet(); Se
11、t dups = new HashSet(); for (int i=0; iargs.length; i+) if (!uniques.add(argsi) dups.add(argsi); uniques.removeAll(dups); System.out.println(Unique words: + uniques); System.out.println(Duplicate words: + dups); D:java FindDups2 i came i saw i leftUnique words: came, left, sawDuplicate words: i D:11
12、HashSet(無序集合)對象集合(Set)import java.util.*;public class SetDemo3 public static void main(String args) boolean b; Set s = new HashSet(); b = s.add(string1); System.out.println(string1 add returns + b); b = s.add(string2); System.out.println(string2 add returns + b); b = s.add(string3); System.out.print
13、ln(string3 add returns + b); b = s.add(string1); System.out.println(string1 add returns + b); b = s.add(string2); System.out.println(string2 add returns + b); Iterator i = s.iterator(); while (i.hasNext() System.out.println(i.next(); D:java SetDemo3string1 add returns truestring2 add returns truestr
14、ing3 add returns truestring1 add returns falsestring2 add returns falsestring3string1string2D:java.util.Iterator迭代器(interface)輪循無序輸出12對象集合(Set)java.util.Set(interface)java.util.SortedSet(interface)java.util.HashSet(class)java.util.TreeSet(class)無序集合有序集合HashSet: Set implemented via a hash table TreeS
15、et: SortedSet implemented as a tree 13java.util.TreeSet implement java.util.SortedSet對象集合(Set)import java.util.*;public class SetDemo4 public static void main(String args) boolean b; Set s = new TreeSet(); b = s.add(string1); System.out.println(string1 add returns + b); b = s.add(string2); System.ou
16、t.println(string2 add returns + b); b = s.add(string3); System.out.println(string3 add returns + b); b = s.add(string1); System.out.println(string1 add returns + b); b = s.add(string2); System.out.println(string2 add returns + b); Iterator i = s.iterator(); while (i.hasNext() System.out.println(i.ne
17、xt(); D:java SetDemo4string1 add returns truestring2 add returns truestring3 add returns truestring1 add returns falsestring2 add returns falsestring1string2string3D:java.util.Iterator迭代器(interface)輪循有序輸出(自然順序)14對象集合(Set)import java.util.*;public class SetDemo5 public static void main(String args) S
18、et s = new HashSet(); s.add(37); s.add(new Integer(37); Iterator i = s.iterator(); while (i.hasNext() System.out.println(i.next(); create a set and add two objects to it; the objects are distinct but have the same displayable stringD:java SetDemo53737D:15對象集合(Set)import java.util.*;public class SetD
19、emo6 public static void main(String args) Set s = new TreeSet(); s.add(37); s.add(new Integer(37); Iterator i = s.iterator(); while (i.hasNext() System.out.println(i.next(); create a SortedSet, add two objects to the set;the objects are not comparableD:java SetDemo6Exception in thread main java.lang
20、.ClassCastExceptionD: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. 16對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enu
21、meration)和迭代(Iterator)小結(jié)第八章 集合操作17有序?qū)ο蟮募螦 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)18java.util.List 接口 (列表與元素)public v
22、oid 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 ind
23、exOf(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)19java.util.List 接口 (列表之間)public boolean addAll(Collection c) Appends all of the elements in the specified collection to the end of this list.pub
24、lic 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 t
25、he elements in this list that are contained in the specified collection.對象列表(List)20java.util.ArrayList implements java.util.Listimport java.util.*;public class ListDemo1 public static void main(String args) List list = new ArrayList(); for (int i = 1; i = 10; i+) list.add(i + * + i + = + i * i); It
26、erator iter = list.iterator(); while (iter.hasNext() System.out.println(iter.next(); D:java ListDemo11 * 1 = 12 * 2 = 43 * 3 = 94 * 4 = 165 * 5 = 256 * 6 = 367 * 7 = 498 * 8 = 649 * 9 = 8110 * 10 = 100D:對象列表(List)21對象列表(List)java.util.List(interface)java.util. AbstractSequentialList(class)java.util.
27、AbstractList(class)java.util.LinkedList(class)java.util.ArrayList(class)ArrayList: List implemented via an array LinkedList: List implemented as a linked structure22對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作23A Map is an object that maps k
28、eys to valuesA 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 (值)MapEntry 1Entry 2Entry n24對象映射(Map)對象列表(List)java.util.Map(interface)java.util.AbstractMap(class)java.util.HashMap(class)HashMap: Map implement
29、ed via a hash table TreeMap: SortedMap implemented as a treejava.util.TreeMap(class)25對象映射(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
30、 isEmpty(); / 批量操作 void putAll(Map t); void clear(); / Collection Views public Set keySet(); public Collection values(); public Set entrySet();public interface Entry Object getKey(); Object getValue(); Object setValue(Object value);java.uitl.Map接口26對象映射(Map)import java.util.*;public class MapDemo1 p
31、rivate static final Integer ONE = new Integer(1); public static void main(String args) Map m = new HashMap(); for (int i=0; iargs.length; i+) Integer freq = (Integer) m.get(argsi); m.put(argsi, (freq=null ? ONE:new Integer(Value() + 1); System.out.println(m.size()+ distinct words detected:); System.
32、out.println(m); java.uitl.HashMap implements java.util.Mapinterface Map public Object get(Object key) public Object put(Object key, Object value)D:java MapDemo1 if it is to be it is up to me to delegate 8 distinct words detected: to=3, me=1, delegate=1, it=2, is=2, if=1, be=1, up=127對象映射(Map)import
33、java.util.*;public class MapDemo2 public static void main(String args) Map m = new HashMap(); m.put(”Mary”, ”123-4567”); m.put(”Larry”, ”234-5678”); m.put(”Mary”, ”456-7890”); Iterator iter = m.entrySet().iterator(); while (iter.hasNext() Map.Entry e = (Map.Entry)iter.next(); System.out.println(e.ge
34、tKey() + ” ” + e.getValue(); java.uitl.HashMap implements java.util.Map28對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作29java.util.Collections類僅僅包含靜態(tài)方法對列表(List)的操作查詢(binary Search)/拷貝(copy)/填充(fill)洗牌(shuffle)/倒轉(zhuǎn)(reverse)/排序(sort)/交換(swap)對集合類
35、(Collection)的操作最大/最小同步處理集合類(Collection)/列表(List)映射圖(Map)/集合(Set)不可修改(只讀)處理集合類(Collection)/列表(List)映射圖(Map)/集合(Set)對對象數(shù)組的操作(Collections)可類比于java.util.Arrays類30利用Collections類排序(指定排序方法)對對象數(shù)組的操作(Collections)import java.util.*;public class SortDemo public static void main(String args) List list = new Arr
36、ayList(); list.add(“abc”); list.add(“DEF”); list.add(“ghi”); Collections.sort(list); Iterator iter = list.iterator(); while(iter.hasNext()System.out.println(iter.next(); Collections.sort(list, new MySort(); iter = list.iterator(); while(iter.hasNext()System.out.println(iter.next();import java.util.*
37、;class MySort implements Comparator public int compare(Object o1, Object o2) String s1 = (String)o1; String s2 = (String)o2; return pareToIgnoreCase(s2); class Collections public static void sort(List list)Sorts the specified list into ascending order, according to the natural ordering of its elemen
38、ts.class Collections public static void sort(List list, Comparator c)Sorts the specified list according to the orderInduced by the specified erface java.util.Comparator public int compare(Object o1, Object o2) public boolean equals(Object obj)31對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(M
39、ap)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作32java.util.Enumeration 接口An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. 兩個方法public boolean hasMoreEl
40、ements() public Object nextElement() 示例枚舉 (Enumeration)for (Enumeration e = v.elements() ; e.hasMoreElements() ;) System.out.println(e.nextElement();33提供枚舉方法的類及方法java.util.Hashtable 類public Enumeration elements()public Enumeration keys()java.util.Vector 類 public Enueration elements()來自 JDK 1.0注意The
41、functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration 枚舉 (Enumeration)34java.util.Iterator接口來自JDK 1.2三個方法public bo
42、olean hasNext() public Object next() public void remove() 示例迭代 (Iterator)static void filter(Collection c) for (Iterator i = c.iterator(); i.hasNext(); ) if (!cond(i.next() i.remove(); 35對數(shù)組對象的操作(Arrays)對象集合(Set)對象列表(List)對象映射(Map)對對象數(shù)組的操作(Collections)枚舉(Enumeration)和迭代(Iterator)小結(jié)第八章 集合操作36小結(jié)接口的層次關(guān)系
43、37java.util.HashSet 類Hash table implementation of the Set interface. The best all-around implementation of the Set interface. java.util.TreeSet 類Red-black tree implementation of the SortedSet interface. java.util.LinkedHashSet 類Hash table and linked list implementation of the Set interface. An inser
44、tion-ordered Set implementation that runs nearly as fast as HashSet. java.util.ArrayList 類Resizable-array implementation of the List interface. (Essentially an unsynchronized Vector.) The best all-around implementation of the List interface. 一般用途38java.util.LinkedList 類Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques). java.util.HashMap 類Hash table implementation of the Map
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年增資協(xié)議合同簽訂流程
- 2025年倉儲貨物出借協(xié)議
- 2025年圣誕節(jié)裝飾協(xié)議
- 2025年商業(yè)責(zé)任不足額保險條款設(shè)定
- 二零二五版木屑生物質(zhì)顆粒燃料研發(fā)與推廣合同4篇
- 二零二五年度木工行業(yè)技術(shù)標(biāo)準(zhǔn)制定合作協(xié)議3篇
- 二零二五年度汽車抵押貸款購車二手車過戶合同
- 二零二五年度科技創(chuàng)業(yè)項(xiàng)目股權(quán)眾籌委托投資合同
- 二零二五年度車輛綠色出行補(bǔ)貼購買合同
- 二零二五年度經(jīng)典實(shí)習(xí)合同(法律事務(wù)實(shí)習(xí))
- 機(jī)電安裝工程安全培訓(xùn)
- 洗浴部前臺收銀員崗位職責(zé)
- 2024年輔警考試公基常識300題(附解析)
- GB/T 43650-2024野生動物及其制品DNA物種鑒定技術(shù)規(guī)程
- 暴發(fā)性心肌炎查房
- 工程質(zhì)保金返還審批單
- 【可行性報告】2023年電動自行車項(xiàng)目可行性研究分析報告
- 五月天歌詞全集
- 商品退換貨申請表模板
- 實(shí)習(xí)單位鑒定表(模板)
- 數(shù)字媒體應(yīng)用技術(shù)專業(yè)調(diào)研方案
評論
0/150
提交評論