集合是给了干嘛的,好像是给了存放对象的
不管删除还是增加,直接影响集合本身
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
//测试不带all的方法
Collection c=new ArrayList();
//接口不能实例化,所以我们用他的子类来实例化
//添加
c.add("hallo");
c.add("word");
c.add("java");
System.out.println(c);
//删除
System.out.println(c.remove("java"));
//删除成功,这些基本功能都是bool类型的,输出会返回ture或者flase;
c.remove("java");
System.out.println(c);
//判断集合是否包含指定元素
System.out.println("contains"+ c.contains("hallo"));
//判断是否为空,有元素就false,没元素就true;
System.out.println("isEmpty"+c.isEmpty());
//元素的个数
System.out.println("size"+c.size());
//清除
c.clear();
System.out.println(c);
}
}
程序运行结果为:
[hallo, word, java]
true
[hallo, word]
containstrue
isEmptyfalse
size2
[]