国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

持有對(duì)象

2019-11-11 03:37:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

鏈表

arrayList:隨機(jī)訪問(wèn)性能好,插入和刪除效率差LinkedList:插入和刪除效率高,隨機(jī)訪問(wèn)性能差

初始化

// 1.使用 Arrays.asList 將數(shù)組轉(zhuǎn)化為容器進(jìn)行初始化,速度最慢List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(1,2,3));// 2.使用 Collection.addAll() 進(jìn)行初始化,速度比第一種方案快,但是只能接受容器作為參數(shù)List<Integer> list2 = new ArrayList<Integer>();list2.addAll(list1);// 3. 使用 Collections.addAll() 進(jìn)行初始化,速度最快且參數(shù)靈活,首選方案List<Integer> list3 = new ArrayList<Integer>();Collections.addAll(list3,1,2,3);// Array.asList() 與 Collections.addAll() 都是可變參數(shù)列表,但是他們有一些區(qū)別List<Snow> snow1 = Arrays.asList(new Powder(),new Crusty(),new Slush());// 這樣創(chuàng)建編譯器會(huì)報(bào)錯(cuò), 方法無(wú)法得到要?jiǎng)?chuàng)建的鏈表的類型信息//List<Snow> snow2 = Arrays.asList(new Powder(),new Light(),new Heavy());List<Snow> snow3 = new ArrayList<Snow>();// 方法從第一個(gè)參數(shù)獲取類型信息Collections.addAll(snow3,new Powder(),new Crusty(),new Slush());class Snow {}class Powder extends Snow {}class Light extends Powder {}class Heavy extends Powder {}class Crusty extends Snow {}class Slush extends Snow {}

打印

// 對(duì)于數(shù)組打印需要使用 Array.toString() 但是容器可以直接打印System.out.PRintln(Arrays.toString(new Integer[]{1,2,3}));System.out.println(list1);

鏈表常用方法

ArrayList<String> arrayList = new ArrayList<String>();arrayList.isEmpty();arrayList.contains("red");arrayList.containsAll(Arrays.asList("red","black"));arrayList.addAll(Arrays.asList("red","blue","green","white","yellow","grey"));arrayList.add("black");//在當(dāng)前位置插入即 2arrayList.add(2,"pink");arrayList.get(2);arrayList.set(2,"red");arrayList.remove(4);//刪除出現(xiàn)的第一個(gè)arrayList.remove("red");arrayList.add("red");arrayList.add("red");// 遍歷,所有的都刪除arrayList.removeAll(Arrays.asList("red","blue"));arrayList.indexOf("red");arrayList.lastIndexOf("red");List<String> subList = arrayList.subList(1,3);System.out.println(subList);System.out.println(subList.indexOf("yellow"));// 對(duì) subList 的修改會(huì)作用到原鏈表subList.add("golden");System.out.println(arrayList);subList.clear();System.out.println(arrayList);

LinkedList 除了與 ArrayList 提供的鏈表常用的方法之外,還提供了對(duì)棧,隊(duì)列,以及雙端隊(duì)列的支持。所以 LinkedList 的方法比 ArrayList 豐富,并且將 LinkedList 向上轉(zhuǎn)型為 List 時(shí),這些這棧,隊(duì)列,雙端隊(duì)列支持的方法也將不能使用。

隊(duì)列

Queue 接口 繼承了 Collection 接口,并在 COllection 接口的基礎(chǔ)上擴(kuò)展了對(duì)容器元素的插入,提取,以及檢查操作。每種操作都提供了兩種不同的方法,當(dāng)相應(yīng)的操作失敗時(shí),一種拋出異常而另一種返回一個(gè)特殊值(null 或者 false)。插入失敗的接口設(shè)計(jì)針對(duì)的是容量大小有限制的隊(duì)列,一般情況下不會(huì)失敗。

拋出異常 返回特殊值
add(e) offer(e)
remove() poll()
element() peek()

在隊(duì)列的使用中,不應(yīng)該插入 null 值,盡管它的實(shí)現(xiàn) LinkedList 允許插入,但是也不應(yīng)該這樣做,應(yīng)為隊(duì)列 poll() 使用 null 作為隊(duì)列為空的標(biāo)志。

雙端隊(duì)列(double ended queue)

拋出異常 返回特殊值
addFirst(e)/addLast(e) offerFirst(e)/offerLast(e)
removeFirst()/removeLast() pollFirst()/pollLast()
getFirst()/getLast() peekFirst()/peekLast()

queue和deque 方法對(duì)比

queue deque
add(e) addFirst(e)
offer(e) offerFirst(e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()

雙端隊(duì)列的接口設(shè)計(jì)時(shí)提供了對(duì)棧的支持,即可以將雙端隊(duì)列作為棧來(lái)操作。而這種用法要優(yōu)于舊的 Stack 類。

stack deque
push(e) addFirst(e)
pop() removeFirst()
peek() peekFirst()

優(yōu)先隊(duì)列

在優(yōu)先隊(duì)列上調(diào)用 offer() 方法插入對(duì)象時(shí),這個(gè)對(duì)象在隊(duì)列內(nèi)會(huì)被排序。自己定義的類需要實(shí)現(xiàn) comparable 或者提供自動(dòng)的比較器。

Set

HashSetTreeSetLinkedHashSet

Map

HashMapTreeMapLinkedHashMap

Collection,Iterator,Iterable

Collection 和 Iterator 提供了面向接口而不是具體實(shí)現(xiàn)的編程方式。除 Map 以外其他的容器都實(shí)現(xiàn)了Collection 接口,而所有的 Collection 都有一個(gè)返回 Iterator 的方法。public static <T> void display(Collection<T> collection){ for(T obj : collection){ System.out.print(obj + " "); } System.out.println();}public static <T> void display(Iterator<T> iterator){ while(iterator.hasNext()){ T obj = iterator.next(); System.out.print(obj + " "); } System.out.println();}public static void main(String[] args){ String[] array = new String[]{"red","blue","black","red"}; List<String> linkedList = new LinkedList<String>(Arrays.asList(array)); Set<String> hashSet = new HashSet<String>(Arrays.asList(array)); CollectionTest.display(linkedList); CollectionTest.display(hashSet); CollectionTest.display(linkedList.iterator()); CollectionTest.display(hashSet.iterator());}

Map 通過(guò) keySet(),values() 可以返回 COllection ,從而與 Collection 建立聯(lián)系。

Collection 可以直接使用 foreach 語(yǔ)法,但是 Iterator 不能。但是兩者各有優(yōu)缺點(diǎn)。Collection 接口可以通過(guò)繼承 AbstractColleciton 類因此有多繼承的限制,而使用 Iterator 只需要在類定義一個(gè)返回該接口實(shí)例的方法。

public class MyCollection extends AbstractCollection<String> { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); CollectionTest.display(myCollection); }}public class MyCollection { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); CollectionTest.display(myCollection.iterator()); }}Iterable 是 java SE5 之后添加的新接口,任何實(shí)現(xiàn)該接口的類都可以使用 foreach 語(yǔ)句,Colleciton 接口繼承了 Iterable 接口。但是數(shù)組雖然可以使用 foreach,但是數(shù)組并沒(méi)有實(shí)現(xiàn) Iterable 接口。public class MyCollection implements Iterable<String> { private String[] array; public MyCollection(){ super(); array = new String[]{"red","blue","black"}; } public Iterator<String> iterator() { return new Iterator<String>() { private int index = 0; public boolean hasNext() { return index < size(); } public String next() { return array[index++]; } }; } public int size() { return array.length; } public static void main(String[] args){ MyCollection myCollection = new MyCollection(); for(String color : myCollection){ System.out.print(color + " "); } }}使用適配器模式顯式的調(diào)用 foreach 語(yǔ)法。

通過(guò)上面的例子可以知道,當(dāng)需要支持 foreach 語(yǔ)法時(shí)可以實(shí)現(xiàn) Iterable 接口。但是假設(shè)我們希望有多個(gè)不同方式來(lái)進(jìn)行迭代,這時(shí)可以采用適配器模式。

public Iterable<String> reversed(){ return new Iterable<String>() { public Iterator<String> iterator() { return new Iterator<String>() { private int i = size(); public boolean hasNext() { return i > 0; } public String next() { return array[--i]; } }; } };}MyCollection myCollection = new MyCollection();for(String color : myCollection){ System.out.print(color + " ");}System.out.println();for(String color : myCollection.reversed()){ System.out.print(color + " ");}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 松原市| 罗定市| 湘阴县| 桂阳县| 拜城县| 平利县| 大关县| 闸北区| 三江| 舟山市| 疏附县| 嘉定区| 八宿县| 灌云县| 大城县| 巴林左旗| 秭归县| 西华县| 嘉荫县| 清流县| 夏河县| 花垣县| 西丰县| 教育| 当阳市| 呼和浩特市| 罗江县| 自治县| 成安县| 四平市| 武清区| 澎湖县| 安龙县| 惠东县| 龙游县| 龙州县| 临猗县| 临沂市| 秀山| 高平市| 会昌县|