數(shù)組工具類arrays和集合collections_第1頁
數(shù)組工具類arrays和集合collections_第2頁
數(shù)組工具類arrays和集合collections_第3頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

付費下載

下載本文檔

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

文檔簡介

1、android培訓(xùn)android培訓(xùn)、java培訓(xùn)ArraysasList方法:將數(shù)組轉(zhuǎn)換成list集合。 arr = abc,kk,= Arrays.asList(arr);/將arr數(shù)組轉(zhuǎn)成list集合。好處呢?用aslist方法,將數(shù)組變成集合;listisEmpty()containsindexOfset;add、remove、clear。(會報不支持操作異常 的是基本數(shù)據(jù)類型,asList會將數(shù)組實體作為集合元素存在。toArraysize,toArray 自size。如果傳遞的指定的類型的數(shù)組的長度大于了集合的 size,那么 toArray 方法,就不會創(chuàng)建新數(shù)組,直接使用該數(shù)組

2、即可,并將集合中的元素 到數(shù)組中,其他為 元素的位置默null。將集合變成數(shù)組后 范例fillsort方法,equalsbinarySearch 方法能對排序好的數(shù)組進(jìn)行二分查找法操importArrayspublicclassTestArrayspublicicvoidarray)if(array!=null)fori=publicclassTestArrayspublicicvoidarray)if(array!=null)fori=0;iarray.length;i+)(arrayi+publicicvoidmain(Stringargs)array=/填充數(shù)Arrays.fill(ar

3、ray,ln(填充數(shù)組:Arrays.fill(array,5):); /將數(shù)組的第2和第3個元素賦值為Arrays.fill(array,2,4,Arrays.fill(array, 2, 4, 8):);array1=/對數(shù)組的第2個到第6個進(jìn)行排序進(jìn)行排ln(對數(shù)組的第 2個到第 6個元素進(jìn)行排序進(jìn)行排序/對整個數(shù)組進(jìn)行排ln(對整個數(shù)組進(jìn)行排序/比較數(shù)組元素是否相l(xiāng)n(比較數(shù)組元素是否等:Arrays.equals(array, array1):+n+Arrays.equals(array, array2=ln(克隆后數(shù)組元素是否等:Arrays.equals(array1, arra

4、y2):+n+Arrays.equals(array1, /使用二分搜索/使用二分搜索算法查找指定元素所在的下標(biāo)(必須是排序好的,否則結(jié)果不確Arrays.binarySearch(array1, 3): +n+Arrays.binarySearch(array1, 3);/如果不存在就返回負(fù)Arrays.binarySearch(array1, 9): +n+Arrays.binarySearch(array1, 9);輸出結(jié)果填充數(shù)組:Arrays.fill(array,5): 5 5 5 5 5將數(shù)組的第23個元素賦值為8:Arrays.fill(array,2,4,5588對數(shù)組的第

5、2個到第 6個元素進(jìn)行排序進(jìn)行排782336125對整個數(shù)組進(jìn)行排23345678比較數(shù)組元素是否相等:Arrays.equals(array,array1): 克隆后數(shù)組元素是否相等:Arrays.equals(array1,array2): 3array1中的位置:Arrays.binarySearch(array1,3): 9array1中的位置:Arrays.binarySearch(array1, -CollectionsCollections.sort(list);/list Collections.sort(list,new ComparatorByLen();/按指定的比較器方

6、法排序。 class ComparatorByLen implements Comparatorcompare(String s1,String temp = s1.length()-s2.length(); Collections.max(list); listindex = Collections.binarySearch(list,zz);/二分查找,返回角標(biāo)。 將非同步集合轉(zhuǎn)成同步集合的方法:CollectionsCollections.max(list); listindex = Collections.binarySearch(list,zz);/二分查找,返回角標(biāo)。 將非同步集合

7、轉(zhuǎn)成同步集合的方法:CollectionsMap synchronizedMap(map);(范例例:對已知集c 進(jìn)行排序?publicclassPracticepublicicvoidmain(StringListc=newArrayList(); 運行結(jié)果為:l,o,v,e,l,o,publicclassPracticepublicicvoidmain(StringListc=newArrayList(); 運行結(jié)果為:l,o,v,l,v, e, o,v,e,運行結(jié)果為:l,o,v,l,v, e, o,v,e,例:binarySearch(Collection,Object)的簡單示例pu

8、blicclassPracticepublicicvoidmain(StringListc=newArrayList(); m=Collections.binarySearch(c,n=-m-運行結(jié)果為:lo,v,1注意:若查找的元素不存在,示例中的 n 即表示該元素最有可能存在的位置的索引4、parator)方法的使用(前者采用 內(nèi)含自然比較法,后者采用 Comparator 進(jìn)行比較)parator)方法的使用(前者采用 Collection 含自然比較法,后者采用 Comparator 進(jìn)行比較)6、indexOfSubList(Listlist,ListsubList)方法的使用(含義

9、:查subListlist中首例publicclassPracticepublicicvoidmain(StringListlist=Arrays.asList(onetwothreefourfivesiven.split(ListsubList=Arrays.asList(threefourfivesix.split(ln(Collections.indexOfSubList(list, 運行結(jié)果為:one,two,three,four,five,six,siven 7、lastIndexOfSubList(List 在此就不做介紹了)ln(Collections.indexOfSubLis

10、t(list, 運行結(jié)果為:one,two,three,four,five,six,siven 7、lastIndexOfSubList(List 在此就不做介紹了)方法的使用與上例方法的使用相8、replaceAll(Listlist,Objectold,Objectnew)方法的使用(含義:替換批定元素為某元素,若要替換的值存在剛返true,反之返false)。例publicclassPracticepublicicvoidmain(StringListlist=Arrays.asList(onetwothreefourfivesiven.split(ListsubList=Arrays.

11、asList(threefourfivesix.split(ln(Collections.replaceAll(list, siven, 運行結(jié)one,two,three,four,five,six,siven one,two,three,four,five,sixsiveneight 例publicclassPracticepublicicvoidmain(StringListlist=Arrays.asList(onetwothreefourfivesiven.split(運行結(jié)one,two,three,four,five,six,siven siven,six,five,four,th

12、ree,two,10、e(Listm)方法的使用(含義:集合中的元素向后移 m publicclassPracticepublicicvoidmain(StringListlist=Arrays.asList(onetwothreefourfivesiven.split(e(list,運行結(jié)one, two, three, four,five, six, siven siven,one,two,Listlist=Arrays.asList(onetwothreefourfivesiven.split(e(list,運行結(jié)one, two, three, four,five, six, sive

13、n siven,one,two,three,four,five,11、copy(Listm,Listn)方法的使用(含義:將集合n中的元素全部例publicclassPracticem 中,并且publicicvoidmain(StringListm=Arrays.asList(onetwothreefourfivesiven.split(ListnArrays.asList(.split(運行結(jié)果為:onetwo,three,four,five,six,我, 叫我, 叫, 12、swap(Listlist,例,four,five,six,j)方法的使用(含義:交換集合中指定元素索引的位置)p

14、ublicclassPracticepublicicvoidmain(StringListm=Arrays.asList(onetwothreefourfivesiven.split(Collections.swap(m,2,運行結(jié)one,two,three,four,five,six,siven one,two,four,three,five,six,13、fill(Listlist,Objecto)方法的使用(含義olist中的所有元素)。publicclassPracticepublicicvoidmain(StringListm=Arrays.asList(onetwothreefourfivesiven.split(Collections.fill(m,運行結(jié)果為one,two,three,publicicvoidmain(StringListm=Arrays.asList(onetwothreefourfivesiven.split(Collections.fill(m,運行結(jié)果為one,two,three,four,five,six,14、n,Object o)方法的使用(含義:返回大小為n 的List,List 不可改變publicclassPracticepublicicvoidmain(Str

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論