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

首頁 > 學院 > 開發設計 > 正文

類集框架(一)

2019-11-14 11:14:35
字體:
來源:轉載
供稿:網友

1.單值保存的最大父接口:Collection

在Collection中定義了15個方法,在所以的方法中,只有兩個方法最為常用:add(),iterator().從開發來講,很少直接使用Collection。

2.允許重復的子接口:LIst

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

2.1 ArrayList

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]

2.2 Vector

例:使用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

3.不允許重復的子接口:Set

public interface Setextends Collection Set子接口完整的繼承了Collection接口,Set子接口常用的兩個子類為HashSet和TreeSet

3.1散列存放的子類:HashSet

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保存數據的時,集合中重復數據沒有保存,并且無序

3.2排序存放的子類:TreeSet

使用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]

TreeSet排序,自定義類排序,使用Comparable

Source>Generate hashSet

4.集合的輸出操作

4種輸出方式:Iterator,ListIterator,Enumeration,foreach

4.1迭代輸出:Iterator

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接口完成

4.2 雙向迭代輸出:ListIterator(了解)

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可以使用,所以在開發中幾乎不會出現

4.3 Enumeration

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

4.4 foreach

例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+" "); } } }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚州市| 洮南市| 资溪县| 永宁县| 门头沟区| 称多县| 旅游| 临泉县| 卢氏县| 米泉市| 固阳县| 东阳市| 敖汉旗| 南郑县| 麟游县| 赞皇县| 依安县| 蚌埠市| 山西省| 安顺市| 浏阳市| 台中市| 大埔县| 洪洞县| 凤山县| 循化| 孟津县| 繁昌县| 永春县| 乐至县| 麻栗坡县| 鄂温| 宜君县| 唐山市| 天峨县| 霍城县| 辽源市| 惠安县| 长岛县| 蒙阴县| 化州市|