笔记

来源:互联网 发布:匡恩网络待遇怎么样 编辑:程序博客网 时间:2024/05/08 03:09
@Test
public void test2() {
Collection coll = new ArrayList();
coll.add(123);
coll.add("AA");
coll.add(new Date());
// coll.add("BB");
// Person p = new Person("MM",22);
// coll.add(p);
coll.add(new Person("MM", 22));
System.out.println(coll);
// 6.contains(object obj);判断集合中是否包含指定的obj元素。如果包含返回ture不包含返回false
// 判断的依据:根据元素所在的类的equals()方法进行判断
// 明确:如果存入集合的元素是自定义的对象,要重写equlas()方法
boolean b1 = coll.contains(123);
System.out.println(b1);
b1 = coll.contains("AA");
System.out.println(b1);
boolean b2 = coll.contains(new Person("MM", 22));
System.out.println(b2);
// 7.containsAll(collect coll);判断当前集合中是否包含coll中所有元素
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(new String("AA"));
boolean b3 = coll.containsAll(coll1);
        System.out.println(b3);
        //8.retainAll(Collection coll); 求当前集合与Coll的共有元素,返回给当前集合
        coll.retainAll(coll1);
        System.out.println(coll);
        //9.remove(Object obj)
        boolean b4 = coll.remove("BB");
        System.out.println(b4);
}


@Test
public void test1() {
Collection coll = new ArrayList();
// 1.size();返回集合中的元素(对象)的个数
System.out.println(coll.size());
// 2. add(object obj);向集合中添加一个元素
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("BB");
System.out.println(coll.size());
// 3.addAll(Collection coll);将形参coll中包含的所有元素添加到当前集合中
Collection coll1 = Arrays.asList(1, 2, 3);
coll.addAll(coll1);
System.out.println(coll.size());
// 4.isEmpty();判断集合是否为空
System.out.println(coll.isEmpty());
// 5.clear();清空集合元素
coll.clear();
System.out.println(coll.isEmpty());
原创粉丝点击