美文网首页
集合框架(Collection集合的基本功能测试)

集合框架(Collection集合的基本功能测试)

作者: 养码哥 | 来源:发表于2018-04-05 12:21 被阅读0次

核心代码:

  package cn.ithelei;

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

public class CollectionDemo {

public static void main(String[] args) {

    // 测试不带ALL的方法
    

    // Collection collection=new Collection() ;//错误,接口不能实例化
    // 创建集合对象
    Collection c = new ArrayList();
    // A:添加 一个元素
    c.add("hello");
    c.add("ithelei");
    
    /**
     *  public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
        return true;
            }
            返回true;说明 这个集合放元素永远都能成功;元素可重复
     */
    //void clear();//移除所有元素
    //c.clear();
    
    //boolean remove(Object o)//移除一个元素
    //c.remove("hello");
    //boolean remove = c.remove("javaEE");
    
    //boolean contains(Object o) 判断collection 包含是否包含指定的元素,则返回 true
    boolean contains = c.contains("hello");
    
    //      boolean isEmpty()判断collection 不包含元素,则返回 true。
    //c.isEmpty();
    
    //  int size()元素的个数。
    //int size = c.size();
    //System.out.println(size);
    System.out.println(c);
    
    
    }

}

相关文章

网友评论

      本文标题:集合框架(Collection集合的基本功能测试)

      本文链接:https://www.haomeiwen.com/subject/rhgrhftx.html