集合的学习
集合中的接口和实现类之间的关系、区别与共性。

collection接口中常用的方法
-
add ():向集合中添加一个新元素。
-
remove():删除一个元素。
-
isEmpty () :判断集合是否为空,为空返回true,不为空返回false
-
toArray() : 将集合转换为一个Object[] 数组
-
size() : 返回集合的元素个数
-
clear():清除集合中所有的元素
package com.colbert.study.day07;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* @Description 测试collection接口中的各种方法
* @Date 2020/7/21 7:54 下午
* @Author colbert
*/
public class CollectionDemo {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
strings.add("张子枫");
strings.add("古力娜扎");
// [张子枫, 古力娜扎]
System.out.println(strings);
strings.remove("张子枫");
//[古力娜扎]
System.out.println(strings);
boolean b = strings.isEmpty();
// false
System.out.println(b);
strings.remove("古力娜扎");
// true
System.out.println(strings.isEmpty());
strings.add("张三");
strings.add("Colbert");
Object[] objects = strings.toArray();
//[张三, Colbert]
System.out.println(Arrays.toString(objects));
strings.clear();
// true
System.out.println(strings.isEmpty());
}
}
网友评论