Collection中的常用功能
boolean add(Object e): 向集合中添加元素
void clear():清空集合中所有元素
boolean contains(Object o):判断集合中是否包含某个元素
boolean isEmpty():判断集合中的元素是否为空
boolean remove(Object o):根据元素的内容来删除某个元素
int size():获取集合的长度
Object[] toArray():能够将集合转换成数组并把集合中的元素存储到数组中
package com.itheima;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList(); // 多态,父类引用指向子类对象
// boolean add(E e):添加元素
System.out.println(c.add("hello"));
System.out.println(c.add("world"));
// void clear():清空集合
// c.clear();
// boolean contains(Object o) :判断集合中是否包含指定元素
System.out.println(c.contains("hello"));
// boolean isEmpty():是否为空
System.out.println(c.isEmpty());
// boolean remove(Object o):删除元素
System.out.println(c.remove("hello"));
// int size() :返回集合中元素的个数
System.out.println(c.size());
// Object[] toArray():将集合转换成一个Object类型的数组
Object[] objs = c.toArray();
for (int i = 0; i < objs.length; i++) {
System.out.println(objs[i]);
}
System.out.println(c);
}
}