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

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

java之容器

2019-11-14 21:35:48
字體:
來源:轉載
供稿:網友
java之容器

先來一張容器的API框架圖,我們在java中所學的所有知識,都是根據下面這張圖來學習的....

容器API:

  1、Collection接口------定義了存儲一組對象的方法,其子接口Set和List分別定義了存儲的方式。

    ①、Set中的數據對象沒有順序且不可以重復。

    ②、List中的數據對象有順序且可以重復。

  2、Map接口定義了存儲“鍵(key)---值(value)映射對”的方法。

Collection接口:

  Collection接口中定義的方法(意思就是只要你實現了Collection接口,你將擁有下面所有方法):

    

Collection方法舉例:

這里要說明的就是集合里面只能裝引用類型的數據。

import java.util.*;public class TestCollection{    public static void main (String args[]){        Collection collection = new ArrayList();        //可以放入不同類型的對象        collection.add("hello");        collection.add(new Person("f1",18));        collection.add(new Integer(100));        System.out.PRintln(collection.size());        System.out.println(collection);    }}class Person{    private String name;     private int age;    public Person(String name,int age){        this.name = name;        this.age = age;    }}

接下來,我們繼續使用上面的例子,說說Collection里面remove()方法的使用:

import java.util.*;public class TestCollection{    public static void main (String args[]){        Collection collection = new HashSet();        //可以放入不同類型的對象        collection.add("hello");        collection.add(new Person("f1",18));        collection.add(new Integer(100));                collection.remove("hello");        collection.remove(new Integer(100));                System.out.println(collection.remove(new Person("f1",18)));        System.out.println(collection);    }}class Person{    private String name;     private int age;    public Person(String name,int age){            this.name = name;        this.age = age;    }    public String getName(){        return name;    }    public int getAge(){        return age;    }    /*public boolean equals(Object obj){        if(obj instanceof Person){            Person person = (Person)obj;            return (name.equals(person.name) && age == person.age);        }        return super.equals(obj);    }    public int hashCode(){        return name.hashCode();    }*/}

執行上面的例子,你會發現我們插入的數據”hello“和new Integer(100)都可以用remove()方法直接刪除,但是對于new person("f1",18)這對象可以用remove()方法直接刪除嗎?答案是不可以的....

容器類對象在調用remove、contains等方法時需要比較對象是否相等,這會涉及到對象類型的equals方法和hashCode方法;對于自定義的類型,需要重寫equals方法和hashCode方法以實現自定義對象相等規則。

  注意,相等的對象應該具有相等的hash Codes

Ieterator接口(簡單說:Iterator就是一個統一的遍歷我們集合中所有元素的方法)

  1、所有實現了Collection接口的容器類都有一個iterator方法用以返回一個實現了Iterator接口的對象。

  2、Iterator對象稱作迭代器,用以方便的實現對容器元素的遍歷實現。

  3、Iterator實現了下列方法:

下面我們寫一個用Iterator遍歷集合元素的方法。(注:程序運行信息輸出順序可能跟我們輸入的順序不一致,這就是Set集合無序的效果)

import java.util.*;public class TestCollection{    public static void main (String args[]){        Collection collection = new HashSet();                collection.add(new Person("zhang",1));        collection.add(new Person("gao",2));        collection.add(new Person("wang",3));        collection.add(new Person("du",4));        collection.add(new Person("liang",5));        collection.add(new Person("li",6));                Iterator iterator = collection.iterator();        while(iterator.hasNext()){            //next()的返回值類型是Object類型,需要轉換為相應類型            Person person = (Person)iterator.next();            System.out.println(person.name);        }    }}class Person{    public String name;     private int age;    public Person(String name,int age){            this.name = name;        this.age = age;    }    public String getName(){        return name;    }    public int getAge(){        return age;    }}

Set接口

  1、Set接口是Collection的子接口,Set接口沒有提供的額外方法,但實現Set接口的容器類中的元素是沒有順序的,而且不可以重復

  2、Set接口可以與數學中”集合“的概念相對應。

  3、J2SDK API中所提供的容器類有HashSet、TreeSet等...

Set方法舉例:

Set方法舉例:

List接口:

  1、List接口是Collection的子接口,實現List接口的容器類中元素是有順序的,而且可以重復。

  2、List容器中元素都對應一個整數型的序號記載其在內容中的位置,可以根據序號存取容器中的元素。

  3、L2SDK所提供的List容器類有ArrayList,LinkedList等...

List 方法舉例:

  List常用算法:

List常用算法舉例:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日照市| 美姑县| 嘉荫县| 新乐市| 茂名市| 大厂| 大竹县| 渝北区| 渭源县| 余庆县| 石楼县| 绍兴市| 奉贤区| 南京市| 克拉玛依市| 普兰店市| 克拉玛依市| 吉木萨尔县| 华宁县| 桂平市| 平湖市| 万山特区| 饶河县| 海口市| 白河县| 五指山市| 绥江县| 大厂| 桃园县| 乌海市| 读书| 游戏| 石狮市| 宁都县| 鹰潭市| 黄龙县| 扬州市| 南丹县| 海南省| 年辖:市辖区| 双桥区|