1. 程式人生 > >Collection集合的基本功能測試學習筆記

Collection集合的基本功能測試學習筆記

lin res 對象 import print 註意 pes emp pty

collectionXxx.java使用了未經檢查或不安全的操作. 註意:要了解詳細信息,請使用 -Xlint:unchecked重新編譯. java編譯器認為該程序存在安全隱患 溫馨提示:這不是編譯失敗,所以先不用理會,等學了泛型你就知道了 add方法如果是List集合一直都返回true,因為List集合中是可以存儲重復元素的 如果是Set集合當存儲重復元素的時候,就會返回false ArrayList的父類的父類重寫toString方法,所以在打印對象的引用的時候, 輸出的結果不是Object類中toString的結果
import java.util.ArrayList;
import java.util.Collection;

import com.heima.bean.Student;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo2_Collection {

    /**
     * * A:案例演示 
        * 
                基本功能演示

                boolean add(E e)
                boolean remove(Object o)
                void clear()
                boolean contains(Object o)
                boolean isEmpty()
                int size()

        * B:註意:
        * 

     */
    public static void main(String[] args) {
        //demo1();
        Collection c = new ArrayList();     
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");

        //c.remove("b");                                        //刪除指定元素
        //c.clear();                                            //清空集合
        //System.out.println(c.contains("b"));                  //判斷是否包含
        //System.out.println(c.isEmpty());
        System.out.println(c.size());                           //獲取元素的個數
        System.out.println(c);
    }

    public static void demo1() {
        Collection c = new ArrayList();                     //父類引用指向子類對象
        boolean b1 = c.add("abc");
        boolean b2 = c.add(true);                           //自動裝箱new Boolean(true);
        boolean b3 = c.add(100);
        boolean b4 = c.add(new Student("張三",23));           
        boolean b5 = c.add("abc");

        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);

        System.out.println(c.toString());
    }

}

Collection集合的基本功能測試學習筆記