1. 程式人生 > 其它 >java——集合與陣列、Collection介面、Iterator介面與foreach迴圈

java——集合與陣列、Collection介面、Iterator介面與foreach迴圈

一、陣列與集合

1.集合與陣列儲存資料概述:

集合、陣列都是對多個數據進行儲存操作的結構,簡稱java容器。
說明:此時的儲存,主要指的是記憶體層面的儲存,不涉及到持久化的儲存(.txt,.jpg,.avi,資料庫中)

2.陣列儲存的特點:

> 一旦初始化以後,其長度就確定了。
> 陣列一旦定義好,其元素的型別也就確定了。比如:String[] arr;int[] arr1;

3.陣列儲存的弊端:

> 一旦初始化以後,其長度就不可修改。
> 陣列中提供的方法非常有限,對於新增、刪除、插入資料等操作,非常不便。同時效率不高。
> 獲取陣列中實際元素的個數的需求,陣列沒有現成的屬性或方法可用

> 陣列儲存資料的特點:有序、可重複。對於無序、不可重複的需求,不能滿足。

4.集合儲存的優點:

解決陣列儲存資料方面的弊端

二、Collection介面

1.單例集合框架結構

|-----Collection介面;單例集合,用來儲存一個一個的物件
* |----List介面;儲存有序的、可重複的資料。 “動態”陣列
* |----ArrayList、LinkedList、Vector
* |----Set介面:儲存有序的、不可重複的資料 高中講的“集合”
* |----HashSet、LinkedHashSet、TreeSet
對應圖示:
2.Collection介面常用方法:
@Test
    public void test1(){
        Collection coll = new ArrayList();
        //add(Object e):將元素e新增到集合coll中
        coll.add("AA");
        coll.add("BB");
        coll.add(123);
        coll.add(new Date());

        //size():獲取新增的元素的個數
        System.out.println(coll.size());

        
//addAll()(Collection coll1):將coll1集合中的元素新增到當前的集合中 Collection coll1 = new ArrayList(); coll1.add(456); coll1.add("cc"); coll.addAll(coll1); System.out.println(coll.size()); System.out.println(coll); //clear():清空集合元素 coll.clear(); //isEmpty():判斷當前集合是否為空 System.out.println(coll.isEmpty()); }


 @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
//        Person p = new Person("Jerry",13);
//        coll.add(p);
        coll.add(new Person("Jerry",13));

        //contains(Object obj):判斷當前集合中是否包含obj
        //我們在判斷時會呼叫obj物件所在類的equals()。
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));
//        System.out.println(coll.contains(p));
        System.out.println(coll.contains(new Person("Jerry", 13)));

        //2.containsAll(Collection coll1):判斷形參coll1中的所有元素是否都存在於當前集合中。
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));

    }
    @Test
    public void test2(){
        //3.remove(Object obj):從當前集合中移除obj元素。
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        coll.remove(123);
        System.out.println(coll);

        coll.remove(new Person("Jerry",13));
        System.out.println(coll);

        //4.removeAll(Collection coll1):差集:從當前集合中移除coll1中所有的元素
        Collection coll1 = Arrays.asList(123,4567);
        coll.removeAll(coll1);
        System.out.println(coll);
    }
    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        //5.retainAll(Collection coll1):交集:獲取當前集合和coll1集合的交集,並返回給當前集合
//        Collection coll1 = Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //equals(Object obj):要想返回true,需要當前集合和形參集合的元素都相同
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new String("Tom"));
        coll1.add(new Person("Jerry",13));
        coll1.add(false);
        System.out.println(coll.equals(coll1));
    }
    @Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        //hashCode():返回當前物件的雜湊值
        System.out.println(coll.hashCode());

        //8.集合 ---->陣列:toArray():
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //拓展:陣列 ---->集合:呼叫Array類的靜態方法asList()
        List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(list);

        //9.iterator():返回Iterator介面的例項,用於遍歷集合元素。放在IteratorTest.java中測試

    }

3.Collection集合與陣列間的轉換

@Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        //hashCode():返回當前物件的雜湊值
        System.out.println(coll.hashCode());

        //8.集合 ---->陣列:toArray():
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //拓展:陣列 ---->集合:呼叫Array類的靜態方法asList()
        List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(list);

        //9.iterator():返回Iterator介面的例項,用於遍歷集合元素。放在IteratorTest.java中測試

    }

4.使用Collection集合儲存物件,要求物件所屬的類滿足:

向Collection介面的實現類的物件中新增資料obj時,要求obj所在類重寫equals()。

三、Iterator介面與foreach迴圈

1.遍歷Collection的兩種方式:
① 使用迭代器Iterator ②foreach迴圈(或增強for迴圈)
2.java.utils包下定義的迭代器介面:Iterator
2.1 說明:

Iterator物件稱為迭代器(設計模式的一種),主要用於遍歷 Collection 集合中的元素。
GOF給迭代器模式的定義為:提供一種方法訪問一個容器(container)物件中各個元
素,而又不需暴露該物件的內部細節。 迭代器模式,就是為容器而生。

2.2 作用:

遍歷集合Collection元素

2.3如何獲取例項:

coll.iterator()返回一個迭代器例項

2.4遍歷的程式碼實現:

@Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        Iterator iterator = coll.iterator();//方式二:不推薦
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }
        //方式三:推薦
        //hasNext():判斷是否還有下一個元素
        while (iterator.hasNext()){
            //next():①指標下移 ②將下移以後集合位置上的元素返回
            System.out.println(iterator.next());
        }
    }

2.5圖示說明

2.6 remove()的使用

//測試Iterator中的remove()
    //如果還未呼叫next()或在上一次呼叫 next 方法之後已經呼叫了 remove 方法,
    //再呼叫remove都會報IllegalStateException
//
內部定義了remove(),可以在遍歷的時候,刪除集合中的元素。此方法不同於集合直接呼叫remove()
    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        Iterator iterator = coll.iterator();
        //刪除集合中“Tom”
        while (iterator.hasNext()){
            Object obj = iterator.next();
            if ("Tom".equals(obj)){
                iterator.remove();
            }
        }
        //遍歷集合
        iterator = coll.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

3.jdk5.0新特性 ----增強for迴圈:(foreach迴圈)

3.1 遍歷集合舉例:



@Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        //for(集合元素的型別 區域性變數 : 集合物件)
        //
        for (Object obj : coll){
            System.out.println(obj);
        }
    }

3.2 說明:

內部仍然呼叫了迭代器

3.3遍歷陣列舉例:
@Test
    public void test2(){
        int[] arr = new int[]{1,2,3,4,5,6};
        //for(陣列元素的型別 區域性變數 : 陣列物件)
        for (int i : arr){
            System.out.println(i);
        }
    }