240/241Collerction集合和常用功能
Collerction集合
常用方法
booLean add(E e); 向集合中添加元素
boolean remove(E e); 删除集合中的某个元素
void clear( ); 清空集合所有的元素
boolean contains(E e); 判断集合中是否包含某个元素
boolean isEmpty( ); 判断集合是否为空
int size(); 获取集合的长度
object[] toArray(); 将集合转成一个数组
public static void main(String[] args) {
//创建集合对象C
Collection<String> coll = new ArrayList<String>();// booLean add(E e);
//向集合中添加元素
coll.add( "乐迪");
coll.add( "卢本伟");
coll.add( "梁志超" ) ;
coll.add( "六舅" ) ;
System.out.println(coll);//乐迪,卢本伟, 梁志超,六舅]
// //boolean remove(E e);
// //删除集合中的某个元素
// boolean result = coll.remove ( "乐迪" );
// System.out.println( result);
// System.out.println(coll);
// // void clear();
// //清空集合所有的元素
// coll.clear( );
// System.out.println(coll);
//
//// boolean contains(E e);
// boolean result1 = coll.contains("六舅");
// System.out.println(result1);
// boolean isEmpty();
System.out.println(coll.isEmpty());
//int size();
//获取集合的长度
//
System.out.println(coll.size() );
//object[ ] toArray ();
//将集合转成一个数组
Object[] arr = coll.toArray();
//遍历数组
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
Collection集合常用功能
java.util.collection接口
所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法任意的单列集合都可以使用collection接口中的方法
共性的方法:
public boolean add(E e):把给定的对象添加到当前集合中。
public void clear():清空集合中所有的元素。
public boolean remove(E e):把给定的对象在当前集合中删除。
public boolean contains(E e):判断当前集合中是否包含给定的对象。
public boolean isEmpty():判断当前集合是否为空。
public int size():返回集合中元素的个数。
public 0bject[] toArray ():把集合中的元素,存储到数组中。
public static void main(String[] args) {
//创建集合对象,可以使用多态
Collection<String> coll = new ArrayList<String>();
System.out.println(coll);//重写了tostring方法[]
//public boolean add(E e):把给定的对象添加到当前集合中。返回值是一个boolean值,一般都返回true,所以可以不用接收
boolean b1 = coll.add("乐迪");
System.out.println( "b1 : "+b1);//b1 :truesystem.out.println(coll);//[乐迪〕
coll.add("卢本伟");
coll.add("你六舅");
coll.add("梁志超");
System.out.println(coll);//[乐迪,卢本伟,你六舅,田七]
//public boolean remove(E e):把给定的对象在当前集合中删除。返回值是一个boolean值,集合中存在元素,删除元素,返回true
//集合中不存在元素,删除失败,返回false
boolean b2 = coll.remove("你六舅");
System.out.println( "b2 : "+b2);//b2 :true
boolean b3 = coll.remove("我是老六");
System.out.println( "b3: "+b3);
System.out.println( coll);//[乐迪,卢本伟,梁志超]
//public boolean contains(E e):判断当前集合中是否包含给定的对象。包含返回true
//不包含返回false*/
boolean b4 = coll.contains("卢本伟");
System.out.println( "b4 : "+b4);//b4 :true
boolean b5 = coll.contains("我是老六");
System.out.println( "b5 : "+b5);//b5 :false
//public boolean isEmpty():判断当前集合是否为空。集合为空返回true,集合不为空返回false
boolean b6 = coll.isEmpty() ;
System.out.println( "b6 : "+b6);//b6:false
// public int size():返回集合中元素的个数。
int size = coll.size();
System.out.println( "size: "+size);
//public object[] toArray();//把集合中的元素,存储到数组中。
Object[]arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//public void clear():清空集合中所有的元素。但是不删除集合,集合还存在
coll.clear();
System.out.println(coll);
System.out.println(coll.isEmpty());
}
public static void main(String[] args) {
//创建集合对象,可以使用多态
Collection<String> coll = new ArrayList<>();
System.out.println(coll);//重写了toString方法[]
/*
public boolean add(E e):把给定的对象添加到当前集合中。
返回值是一个Boolean值,一般都返回true,所以可以不用接受。
*/
boolean b1 = coll.add("a1");
System.out.println("b1:"+b1);//true
System.out.println(coll);//[乐迪]
coll.add("a2");
coll.add("a3");
coll.add("a4");
coll.add("a5");
coll.add("a6");
coll.add("a7");
coll.add("a8");
System.out.println(coll);//[a1, a2, a3, a4, a5, a6, a7, a8]
/*
public boolean remove(E e):把给定的对象在当前集合中删除。
返回值是一个Boolean,集合中存在元素,删除元素,返回true
集合中不存在元素,删除失败,返回false
*/
boolean b2 = coll.remove("a3");
System.out.println("b2:"+b2);//b2:true
boolean b3 = coll.remove("a9");
System.out.println("b3:"+b3);//b3:false
System.out.println(coll);//[a1, a2, a4, a5, a6, a7, a8]
/*
public boolean contains(E e):判断当前集合中是否包含给定的对象。
包含返回true
不包含返回false
*/
boolean b4 = coll.contains("a4");
System.out.println("b4:"+b4);//b4:true
boolean b5 = coll.contains("a10");
System.out.println("b5:"+b5);//b5:false
/*
public boolean isEmpty():判断当前集合是否为空,集合为空返回true,集合不为空返回false
*/
boolean b6 = coll.isEmpty();
System.out.println(b6);//false
/*
public int size():返回集合中元素的个数。
*/
int size = coll.size();
System.out.println("size:"+size);//size:7
/*
public Object[] toArry():把集合中的元素,存储到数组中。
*/
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
if(i == arr.length-1){
System.out.println(arr[i]);
}else {
System.out.print(arr[i]+" ");//a1 a2 a4 a5 a6 a7 a8
}
}
/*
public void clear():清空集合中所有的元素,但是不删除集合,集合还存在
*/
coll.clear();
System.out.println(coll);//[]
System.out.println("是否为空"+coll.isEmpty());//是否为空:true
System.out.println("size:"+coll.size());//size:0
}