在Collection中定義了15個(gè)方法,在所以的方法中,只有兩個(gè)方法最為常用:add(),iterator().從開(kāi)發(fā)來(lái)講,很少直接使用Collection。
public interface List extends Collection List接口繼承了Collection接口,但是List接口對(duì)Collection接口進(jìn)行了大量的擴(kuò)充。
| No | 方法名稱(chēng) | 描述 |
|---|---|---|
| 1 | public E get(int index) | 取得指定索引位置上的數(shù)據(jù) |
| 2 | public E set(int index,E element) | 修改指定索引位置上的數(shù)據(jù) |
| 3 | public ListIteratorlistIterator | 為L(zhǎng)istIterator接口實(shí)例化 |
List接口有兩個(gè)常用的子類(lèi)ArrayList和Vector
public class ArrayList extends AbstractList implements List ,Randomaccess,Coloneable,Serializable 使用ArrayList的主要目的是為L(zhǎng)ist接口實(shí)例化,所以操作方法都以List接口為主
例:使用ArrayList進(jìn)行List接口的功能驗(yàn)證 import java.util.ArrayList; import java.util.List; public class test{ public static void main(String[] args) { List<String>all=new ArrayList<String>();//實(shí)例化List接口 all.add("hello");//添加內(nèi)容 all.add("hello"); all.add("World"); for(int x = 0 ;x<all.size();x++){ System.out.PRintln(all.get(x)); } 程序運(yùn)行結(jié)果:[hello, hello,World]
例:使用Vector進(jìn)行List接口的功能驗(yàn)證 import java.util.Vector; import java.util.List; public class test{ public static void main(String[] args) { List<String>all=new Vector<String>();//實(shí)例化List接口 all.add("hello");//添加內(nèi)容 all.add("hello"); all.add("World"); for(int x = 0 ;x<all.size();x++){ System.out.println(all.get(x)); } 程序運(yùn)行結(jié)果:[hello, hello,World]
ArrayList 和Vector 都是List接口的子類(lèi)
ArrayList和Vector區(qū)別
| No | 區(qū)別 | ArrayList | Vector |
|---|---|---|---|
| 1 | 推出時(shí)間 | JDK1.2 | JDK1.0 |
| 2 | 性能 | 采用異步處理方式,性能更高 | 采用同步處理方式,性能相對(duì)較低 |
| 3 | 安全性 | 非線程安全 | 線程安全 |
| 4 | 輸出 | Iterator、ListIterator、foreach | Iterator、ListIterator、foreach、Enumeration |
public interface Setextends Collection Set子接口完整的繼承了Collection接口,Set子接口常用的兩個(gè)子類(lèi)為HashSet和TreeSet
HashSet使用一種散列(無(wú)序)的方式保存集合數(shù)據(jù)
例:使用Set接口 import java.util.HashSet; import java.util.Set; public class test{ public static void main(String[] args) { Set<String> all=new HashSet<String>(); all.add("hello"); all.add("hello"); all.add("world"); System.out.println(all); } } 程序運(yùn)行結(jié)果:[world, hello]
使用Set保存數(shù)據(jù)的時(shí),集合中重復(fù)數(shù)據(jù)沒(méi)有保存,并且無(wú)序
使用TreeSet import java.util.Set; import java.util.TreeSet; public class test{ public static void main(String[] args) { Set<String> all=new TreeSet<String>(); all.add("c"); all.add("b"); all.add("a"); System.out.println(all); } } 程序運(yùn)行結(jié)果:[a, b, c]
Source>Generate hashSet
4種輸出方式:Iterator,ListIterator,Enumeration,foreach
Iterator中常用方法
| No | 方法名稱(chēng) | 描述 |
|---|---|---|
| 1 | public boolean hasNext() | 判斷是否有下一個(gè)元素 |
| 2 | public E next() | 取出當(dāng)前元素 |
| 3 | public void remove() | 移除當(dāng)前元素 |
如何取得Iterator的實(shí)例化對(duì)象?Collection繼承了一個(gè)Iterator接口,在Iterator接口中定義了一個(gè)方法“public Iteratoriterator()”
例:使用Iterator輸出集合數(shù)據(jù) import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); Iterator<String>iter =all.iterator(); while(iter.hasNext()){ String str = iter.next(); System.out.print(str+" "); } } } 程序運(yùn)行結(jié)果:hello hello world
在項(xiàng)目開(kāi)發(fā)中,只要遇到集合對(duì)象輸出問(wèn)題,一定要使用Iterator接口完成
Iterator可以完成由前向后的單向輸出操作,如果希望完成由前向后和由后向前輸出的話可以利用ListIterator接口完成
ListIterator接口的擴(kuò)充方法
| No | 方法名稱(chēng) | 描述 |
|---|---|---|
| 1 | public boolean hasprevious() | 判斷是否有前一個(gè)元素 |
| 2 | public E previous() | 取出前一個(gè)元素 |
如果要取得ListIterator接口的實(shí)例化對(duì)象,只能后依靠List接口,在List接口中存在方法,為L(zhǎng)istIterator接口實(shí)例化:public ListIteratorlistIterator()
例:執(zhí)行雙向迭代 import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); ListIterator<String>iter =all.listIterator(); System.out.print("從前向后輸出"); while(iter.hasNext()){ String str = iter.next(); System.out.print(str+" "); } System.out.print("/n"+"從后向前輸出"); while(iter.hasPrevious()){ String str =iter.previous(); System.out.print(str+" "); } } } 程序運(yùn)行結(jié)果: 從前向后輸出hello hello world 從后向前輸出world hello hello
對(duì)于由后向前的輸出操作,在進(jìn)行前一定要首先發(fā)生由前向后的輸出。由于此輸出接口只有List可以使用,所以在開(kāi)發(fā)中幾乎不會(huì)出現(xiàn)
Enumeration是最早的輸出接口,最早稱(chēng)為枚舉,在JDK1.0時(shí)已經(jīng)推出,在JDK1.5的時(shí)候進(jìn)行了擴(kuò)充,主要增加了泛型,在Enumeration接口里面只定義了兩個(gè)方法
| No | 方法名稱(chēng) | 描述 |
|---|---|---|
| 1 | public boolean hasMoreElements() | 判斷是否有下一個(gè)值 |
| 2 | public E nextElement() | 取出當(dāng)前元素 |
要取得Enumeration的實(shí)例化對(duì)象,不能依靠Collection接口,只能依靠Vector,在Vector中定義了方法public Enumreationelements()
例:使用Enumreation進(jìn)行輸出 import java.util.Enumeration; import java.util.Vector; public class test{ public static void main(String[] args) { Vector<String> all=new Vector<String>(); all.add("hello"); all.add("hello"); all.add("world"); Enumeration<String>enu =all.elements(); System.out.print("從前向后輸出"); while(enu.hasMoreElements()){ String str = enu.nextElement(); System.out.print(str+" "); } } } 程序結(jié)果輸出:從前向后輸出hello hello world
例foreach import java.util.ArrayList; import java.util.List; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); for(String str:all){ System.out.println(str+" "); } } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注