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

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());
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章