package cn.itcast.p3.collection.demo;
import java.util.ArrayList;//导入ArrayList的包0
import java.util.Collection;//导入Collection的包
public class CollectionDemo {
/**
* @param args
*/
public static void main(String[] args) {
//ArrayList coll = new ArrayList();
// show(coll);
Collection c1 = new ArrayList();//new一个Collection对象,注意Collection是接口,只能使用子类List的子类ArrayList创建对象
Collection c2 = new ArrayList();
show(c1,c2);
}
public static void show(Collection c1,Collection c2){
//给c1添加元素。
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
//给c2添加元素。
c2.add("abc1");
c2.add("abc2");
c2.add("abc3");
c2.add("abc4");
c2.add("abc5");
System.out.println("c1:"+c1);//输出Collection对象会直接输出对象里面的所有元素
System.out.println("c2:"+c2);
//演示addAll
// c1.addAll(c2);//将c2中的元素添加到c1中。
//演示removeAll
boolean b = c1.removeAll(c2);//将两个集合中的相同元素从调用removeAll的集合中删除。
// System.out.println("removeAll:"+b);
//演示containsAll
// boolean b = c1.containsAll(c2);
System.out.println("containsAll:"+b);
//演示retainAll
// boolean b = c1.retainAll(c2);//取交集,保留和指定的集合相同的元素,而删除不同的元素。
//和removeAll功能相反 。
// System.out.println("retainAll:"+b);
System.out.println("c1:"+c1);
}
/*public static void show(Collection coll){
//1,添加元素。add.
coll.add("abc1");
coll.add("abc2");
coll.add("abc3");
System.out.println(coll);
//2,删除元素。remove
// coll.remove("abc2");//会改变集合的长度
//清空集合.
// coll.clear();
System.out.println(coll.contains("abc3"));
System.out.println(coll);
}*/
}
以上的代码显示了如何使用Collection创建集合框架,并且增加、删除和输出其中的元素的
集合框架
最新推荐文章于 2019-12-21 21:07:11 发布