Java分布式學習資料:lesson-8- Java Data Collection_第1頁
Java分布式學習資料:lesson-8- Java Data Collection_第2頁
Java分布式學習資料:lesson-8- Java Data Collection_第3頁
Java分布式學習資料:lesson-8- Java Data Collection_第4頁
Java分布式學習資料:lesson-8- Java Data Collection_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、Java Data Collections圖示單擊此處添加段落文字內(nèi)容1dsds此處添加段落文字內(nèi)容2單擊此處添加段落文字內(nèi)容3單擊此處添加段落文字內(nèi)容4Java Data Collection 集合數(shù)據(jù)類型是Java中常用的包,包括Collection和Map兩個接口的實現(xiàn)類。Collection用于存放多個單對象,Map用于存放Key-Value形式的鍵值對。 Collection包括List和Set接口。List支持存放重復對象,Set不支持。 List接口實現(xiàn)的類有ArrayList,LinkedList,Vector和Stack。 Contd. Set接口實現(xiàn)的類有HashSet,T

2、reeSetVector。 Collection的常用操作包括,創(chuàng)建Collection對象、增加對象(add)、刪除對象(remove)、獲取單個對象(get)、遍歷(用Iterator獲取迭代器,進而遍歷)、判斷是否包含對象(contains)、排序等。 ArrayList ArrayList基于數(shù)組實現(xiàn),沒容量限制。用函數(shù)trimToSize縮小容量。ArrayList常用的操作包括創(chuàng)建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在ArrayList中、判斷對象在元素中的位置等操作。ArrayList ArrayList基于數(shù)組實現(xiàn),沒容量限制。用函數(shù)trimToSize縮小容

3、量。 ArrayList Example (Fragment) ArrayList objTest = new ArrayList(); for(int step = 0; step10; step+) objTest.add(step*step); Iterator ite = objTest.iterator(); while(ite.hasNext() System.out.println(ite.next(); Contd. Scanner indata = new Scanner(System.in); int searchdat = indata.nextInt(); if(obj

4、Test.contains(searchdat) System.out.println(Searched the required data.); else System.out.println(Not searched the required data.s); LinkedList LinkedList基于數(shù)組實現(xiàn),沒容量限制。用函數(shù)trimToSize縮小容量。 LinkedList常用的操作包括創(chuàng)建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在LinkedList中、判斷對象在元素中的位置等操作。LinkedList Example (Fragment) LinkedList

5、 linkedlstTest = new LinkedList(); for(int step = 0; step10; step+) linkedlstTest.add(step*step); System.out.println(共有 + linkedlstTest.size() + 個元素); System.out.println(第一個元素是 + linkedlstTest.getFirst(); System.out.println(最后一個元素是 + linkedlstTest.getLast(); Contd. Iterator llIte = linkedlstTest.ite

6、rator(); while(llIte.hasNext() System.out.println(llIte.next(); Vector and Stack Vector操作與ArrayList類似 Vector常用的操作包括創(chuàng)建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在Vector中、判斷對象在元素中的位置等操作。 Stack的常用操作包括push、pop和peek(獲取最后一個元素)。 HashMap HashMap操作創(chuàng)建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在Vector中、判斷對象在元素中的位置等操作。 Put(Object key, Object

7、Value); Remove(Object key); Contains(Object key) get(Object key); HashMap (Fragment) HashMap hashmapTest = new HashMap(); for(int step = 0; step 10; step+) String temp = null; temp = String.format(%d, step); hashmapTest.put(temp, step*10.0); Scanner scan = new Scanner(System.in); int index = scan.ne

8、xtInt();HashMap (Fragment) String tmpkey = String.format(%d, index); if(hashmapTest.containsKey(tmpkey) System.out.println(found the key); else System.out.println(Not found the key); Iterator ite; ite = hashmapTest.keySet().iterator(); while(ite.hasNext() HashMap (Fragment) String tempkey = (String)ite.next(); System.out.println(Key + tempkey + value : + hashmapTest.get(tempkey); ite = hashmapTest.entrySet().iterator(); while(ite.hasNext() Map.Entry entry = (Map.Entry) ite.next(); String key = entry.getKey(); Double value = entry.getValue(); System.out.println(Key + key + value : +

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論