麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

Java基礎--容器(1)

2019-11-14 10:38:34
字體:
來源:轉載
供稿:網友
1.容器?容器:java API所提供的一系列類的實例,用于在程序中存放對象。?

J2SDK所提供的容器API位于java.util包內,容器API的類圖結構如下:?

左邊是一個一個裝,右邊是一對一對來裝?Set的數據對象沒有順序并且不可以重復,List有順序可以重復(互相的equals)?Map接口定義了存儲“鍵(key)—值(value)映射對”的方法?2.Collection接口?Collection接口中所定義的方法:?int size();                               

boolean isEmpty();?void clear();                           

boolean contains(Object element);?      是不是包含某個對象boolean add(Object element);?boolean remove(Object element);?        去除Iterator iterator();?boolean containsALL(Collection c);?     是不是包含另一個集合中的所有元素boolean addALL(Collection c);??boolean removeALL(Collection c);??boolean retainALL(Collection c);??          求交集Object[] toArray();?例如:?import java.util.*;

public class Name {    PRivate String s;    private String c;    public Name(String s, String c) {        this.s = s;        this.c = c;    }    public String toString() {        return s + " " + c;    }}public class Cllection {    public static void main(String agrs[]) {        Collection c = new ArrayList(); //使用父類引用指向子類對象,可以放入不同類型的對象        c.add("hello");        c.add(new Integer(100)); //只能添加對象,不能添加基礎的數據類型        c.add(new Name("f1", "l1"));        System.out.println(c.size());        System.out.println(c);    }}結果是:3      [hello, 100, f1 l1]?3.Collection方法舉例?容器類對象在調用remov、contains等方法時需要比較對象是否相等,這回涉及到對象類型的equals方法和hashCode?方法;對于自定義的類型,需要重寫equals方法以實現自定義的對象相等規則。?注意:相等的對象應該具有相等的hash Codes。?重寫equals方法時同時也要重寫hashCode。增加Name類的equals和hashCode方法如下:??
public class Name {    private String s;    private String c;    public Name(String s, String c) {        this.s = s;        this.c = c;    }    public String getS() {        return s;    }    public String getC() {        return c;    }    public String toString() {        return s + " " + c;    }    public boolean equals(Object obj) {        if (obj instanceof Name) {            Name name = (Name) obj;            return (s.equals(name.s))                    && (s.equals(name.s));        }        return super.equals(obj);    }    public int hashCode() { //地址,索引        return s.hashCode();    }}
public class Cllection {    public static void main(String agrs[]) {        Collection c = new HashSet(); //使用父類引用指向子類對象,可以放入不同類型的對象        c.add("hello");        c.add(new Integer(100)); //只能添加對象,不能添加基礎的數據類型        c.add(new Name("f1", "l1"));        c.remove("hello");        c.remove(new Integer(100));        System.out.println(c.remove(new Name("f1", "l1")));        //equals時才去除,返回true,這兩個不是同一個對象        System.out.println(c);    }}結果是:true   []?如果沒有重寫equals方法時,結果是:?false  [f1   l1]4.Iterator接口?所有實現了Collection接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象。Iterator是?統一的來遍歷Collection里所有元素的方法Iterator對象稱作迭代(重復反饋過程的活動)器,用以方便的實現對容器內元素的遍歷操作。?Iterator接口定義了如下方法:?boolean hasNext();       //判斷游標右邊是否有元素?Object next();                 //返回游標右邊的元素并將游標移動到下一個位置?void remove();               //刪除游標左邊的元素,在執行完next之后該操作只能執行一次?
public class TestIterator {    public static void main(String agrs[]) {        Collection c = new HashSet();        c.add(new Name("f1", "l1"));        c.add(new Name("f2", "l2"));        c.add(new Name("f3", "l3"));        Iterator i = c.iterator();        while (i.hasNext()) {            Name n = (Name) i.next();            System.out.println(n.getS() + " ");        }    }}結果是:f1    f2     f3Iterator對象的remove方法是在迭代過程中刪除元素的唯一的安全方法。例:
public class TestIterator {    public static void main(String agrs[]) {        Collection c = new HashSet();        c.add(new Name("f1", "1111"));        c.add(new Name("f2", "l2"));        c.add(new Name("f3", "1113"));        for (Iterator i = c.iterator(); i.hasNext(); ) {            Name n = (Name) i.next();            if (n.getC().length() < 3) {                i.remove();            }        }        System.out.println(c);    }}結果是:[f1 1111, f3 1113]?5.JDK1.5增強的for循環?增強的for循環對于遍歷array或collection的時候相當簡便?缺陷:數組:不能方便的訪問下標值?集合:與使用Iterator相比,不能方便的刪除集合中的內容,在內部也是調用Iterator?出了簡單遍歷并讀出其中的內容外,不建議使用增強for?例:
public class TestEnhanceFor {    public static void main(String agrs[]) {        Collection c = new HashSet();        c.add(new Name("a", "aa"));        c.add(new Name("b", "bb"));        c.add(new Name("c", "cc"));        for (Object o : c) {            System.out.println(o);        }    }}結果:a aa       

b bb              

c cc6.Set接口?Set接口是Collection的子接口,Set接口沒有提供額外的方法,但實現Set接口的容器類中的元素是沒有順序的,而且不可以重復。?Set容器可以與數學中“集合”的概念相對應,有HashSet,TreeSet等??

public class TestSet {    public static void main(String ages[]) {        Set s = new HashSet();        s.add("hello");        s.add("world");        s.add(new Name("f1", "l1"));        s.add(new Integer(100));        s.add(new Name("f1", "l1"));//如果Name中沒有重寫equals方法,該項就會被添加,如果重寫了,就不會添加        s.add("hello"); //相同元素不會被加入        System.out.println(s);    }}結果是:

[world, 100, hello, f1 l1]?

public class TestSet {    public static void main(String ages[]) {        Set s1 = new HashSet();        Set s2 = new HashSet();        s1.add("a");        s1.add("b");        s1.add("c");        s2.add("a");        s2.add("d");        s2.add("b");//Set和List容器類都具有        //Collection(Collection c)構造方法用于初始化容器類        Set sn = new HashSet(s1);        sn.retainAll(s2);                             //交集        Set su = new HashSet(s1);        su.addAll(s2);        System.out.println(sn);        System.out.println(su);    }}結果是:

[a, b]      

[a, b, c, d]


上一篇:springMVC簡單學習

下一篇:一只小蜜蜂...

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 性欧美性欧美 | 欧美成人免费一区二区三区 | 亚洲影院在线播放 | 5a级毛片| 黄色1级视频 | 看全色黄大色黄大片女图片 | 黄色片网站免费在线观看 | 欧美亚洲国产一区二区三区 | 九九热这里只有精品8 | 免费在线看a | 韩国精品久久久 | 欧美精品亚洲人成在线观看 | 密室逃脱第一季免费观看完整在线 | 国产精品视频一区二区三区四区五区 | 成人免费影院 | 精品国产一级毛片 | porno video hd 365hd | 久久骚| 成人做爰高潮片免费视频美国 | 免费一级欧美大片视频 | 91懂色| 久久久久久高清 | 99国产精品国产免费观看 | 国产日韩成人 | 视频一区 中文字幕 | 毛片在线视频在线播放 | 黄网站免费观看视频 | 黄色毛片一级视频 | 92精品国产自产在线 | 国产一区二区成人在线 | 国产成人高潮免费观看精品 | 精品一区二区亚洲 | 激情在线观看视频 | 国产男女 爽爽爽爽视频 | 黄色片视频观看 | av免费在线播放网址 | 精品国内视频 | 久久久久久久久久久久久久av | 国产精品久久久久久久久久免 | 成人小视频在线播放 | 亚洲国产网站 |