Java_基础—Collection集合的基本功能测试

package com.soar.collection;

import java.util.ArrayList;
import java.util.Collection;

import com.soar.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:注意:
        collectionXxx.java使用了未经检查或不安全的操作.
        注意:要了解详细信息,请使用 -Xlint:unchecked重新编译.
        java编译器认为该程序存在安全隐患
        温馨提示:这不是编译失败,所以先不用理会,等学了泛型你就知道了

add(方法)如果是List集合一直都返回true,因为List集合中可以存储重复元素的
        如果是Set集合当存储重复元素的时候,就返回false
ArrayList的父类的父类重写了toString方法,所以在打印对象的引用的时候,输出的结果不是Object类中toString的结果

 */
    public static void main(String[] args) {
        //addFunction();
        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);
    }

private static void addFunction() {
    Collection c = new ArrayList();     //父类引用指向子类对象,编译看父类,运行看子类
    boolean b1 = c.add("abc");
    boolean b2 = c.add(true);       //自动装箱, new Boolean(true)
    boolean b3 = c.add(100);        //自动装箱, new Integer(100)
    boolean b4 = c.add(new Student("张三",23));
    boolean b5 = c.add("abc");

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

    System.out.println(c);  //[abc, true, 100, Student [name=张三, age=23], abc]
    System.out.println(c.toString());   //同上
}
}
posted @ 2017-07-13 10:06  Soar_Sir  阅读(149)  评论(0编辑  收藏  举报