Collection 1

来源:互联网 发布:linux怎么解压tgz文件 编辑:程序博客网 时间:2024/06/05 08:57
package niu.cheng1;


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


/*
 * Collection 层次结构 中的根接口
 * 
 * 添加功能
 * boolean add(E e)确保此 collection 包含指定的元素(可选操作)。
 * 判断功能
 * boolean contains(Object o)如果此 collection 包含指定的元素,则返回 true
 * boolean isEmpty()如果此 collection 不包含元素,则返回 true。 
 * 删除功能
 * void clear()移除此 collection 中的所有元素(可选操作)。
 * boolean remove(Object o)从此 collection 中移除指定元素的单个实例
 * 长度功能
 * int size()返回此 collection 中的元素数。


 */
public class CollectionDemo {
public static void main(String[] args) {
Collection c=new ArrayList();
System.out.println("c:"+c);//c:[]

//boolean add(E e)确保此 collection 包含指定的元素(可选操作)。
//System.out.println("add:"+c.add("hello"));
c.add("hello");
c.add("world");
c.add("java");
System.out.println("c:"+c);//c:[hello, world, java]
/*
//boolean remove(Object o)从此 collection 中移除指定元素的单个实例
c.remove("hello");
System.out.println("c:"+c);//c:[world, java]

//void clear()移除此 collection 中的所有元素(可选操作)。
c.clear();
System.out.println("c:"+c);//c:[]
*/

/*
//boolean contains(Object o)如果此 collection 包含指定的元素,则返回 true
System.out.println("c:"+c.contains("hello"));//c:true
System.out.println("c:"+c.contains("hahah"));//c:false

//boolean isEmpty()如果此 collection 不包含元素,则返回 true。
System.out.println("c:"+c.isEmpty());//c:false
c.clear();
System.out.println("c:"+c.isEmpty());//c:true
*/

//int size()返回此 collection 中的元素数
System.out.println("c:"+c.size());//c:3

//
}
}
0 0