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

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

JAVA_Collection容器

2019-11-14 20:59:59
字體:
來源:轉載
供稿:網友
java_Collection容器

  因為項目的需要,今天抽時間把JAVA中的容器復習了一下,為了以后的不時之需,現在把它記下來。

  容器有其名,知其意,用來盛放數據的集合,JAVA中為我們提供了三種容器類:set、list、map,三種容器之間既有聯系又有區別,首先它們均繼承了Collection容器,區別在于:set容器存儲數據類似于集合,里面的數據之間沒有順序,不能重復;list容器中的數據有序,并且數據可以重復;最后map容器是一種通過鍵值對進行的存儲,所以map容器要求鍵值不能重復。

  通過這個圖相信大家一定能夠對JAVA容器有一個很好地認識。

接下來讓我們一起看幾個例子:

第一個:HashSet、LinkedList、ArrayList、Interator的介紹

public class hashset {        public static void main(String[] args) {        Collection c = new HashSet();        c.add("one");        c.add("two");        c.add("three");        c.add("four");        c.add("five");        Iterator it = c.iterator();        while(it.hasNext()){            System.out.PRintln(it.next());        }    }}

輸出結果:(HashSet存儲里面的數據是無序的)

public class linkedlist {    public static void main(String[] args) {        Collection c = new LinkedList();        c.add("one");        c.add("two");        c.add("three");        c.add("four");        c.add("five");        Iterator it = c.iterator();        while(it.hasNext()){            System.out.println(it.next());        }    }}

輸出結果:

public class hashset {        public static void main(String[] args) {        Collection c = new HashSet();        c.add("one");        c.add("two");        c.add("three");        c.add("four");        c.add("five");        Iterator it = c.iterator();        while(it.hasNext()){            System.out.println(it.next());        }    }}

輸出結果:

public class object_interator {    public static void main(String [] args){        Collection c = new ArrayList();                //特別注意,add添加的均要為Object對象        c.add(new student("張生", "男"));        c.add(new student("王二", "男"));        c.add(new student("莉莉", "女"));        c.add(new student("小明", "男"));                Iterator it = c.iterator();        while(it.hasNext()){            student stu = (student)it.next();//特別注意it.next()獲得的是一個Object對象,一定要轉化為指定的對象,然后進行操作            System.out.println(stu);//默認調用其toString()方法        }    }}//定義的一個student對象class student{    public String name;    public String sex;        //無參構造方法    public student(){}    //有參構造方法    public student(String name, String sex){        this.name = name;        this.sex = sex;    }        public String getname(){        return name;    }        public String getsex(){        return sex;    }        //從寫其toString()方法    public String toString(){        return "姓名:"+name+" 性別:"+sex;    }    }

下面簡單介紹一下SDK1.5提出的增強for循環:

public class addFor {    public static void main(String[] args) {        int arr [] = {1,2,3,4,5};        for(int i=0; i<arr.length;i++){            System.out.println("傳統的輸出:"+arr[i]);        }        System.out.println("");        for(int i : arr){            System.out.println("增強的for循環輸出:"+i);        }        System.out.println("");        Collection c = new ArrayList();        c.add(new String("aaa"));        c.add(new String("bbb"));        c.add(new String("ccc"));        c.add(new String("ddd"));        for(Object o : c){            System.out.println(o);//默認調用其toString()方法        }    }}

對于List容器JAVA給出了一種處理內部數據的方法:Collections,下面簡單給大家分享一下我的理解:

public class list_fix {    public static void main(String [] args){        List li = new ArrayList();        for(int i = 0; i<=5; i++){            li.add("a"+i);        }        System.out.println("處理前:"+li);        Collections.reverse(li);//逆序排列        System.out.println(li);        Collections.shuffle(li);//隨機排列        System.out.println(li);        Collections.sort(li);//排序        System.out.println(li);        int n = Collections.binarySearch(li, "a5");//基于二分法的查找        System.out.println("a5的位置:"+n);                    }}

輸出結果:

到這里我想大家估計已經對容器有了一定的了解,如果你有更好的認識還望大家賜教。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清原| 辽中县| 卢湾区| 镇宁| 临漳县| 博野县| 西峡县| 吕梁市| 葫芦岛市| 宁晋县| 镇赉县| 阿图什市| 拜泉县| 新巴尔虎右旗| 莱西市| 泰兴市| 温州市| 莫力| 密山市| 确山县| 本溪| 深泽县| 府谷县| 涡阳县| 荔浦县| 格尔木市| 安义县| 元氏县| 陇南市| 延长县| 察雅县| 高陵县| 石渠县| 阳朔县| 蕉岭县| 孙吴县| 崇文区| 突泉县| 华容县| 资中县| 沙坪坝区|