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