Collection集合中有八个最基本的方法
package com.ys.fanxin17;
import java.util.ArrayList;
import java.util.Collection;
/*
* Collection
* boolean add(E e)
* void clear()
* boolean contains(Object o)
* boolean isEmpty()
* boolean remove(Object o)
* int size()
* Object[] toArray()
*
*
* Iterator<E> iterator()
*/
public class CollectionDemo02 {
public static void main(String[] args) {
//创建集合对象
// Collection c = new Collection();//Collection是接口,不能实例化
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("java"));
// int size():返回集合中的元素个数
// System.out.println(c.size());
// Object[] toArray():将集合转化成Object类型的数组
Object[] o = c.toArray();
//数组是length属性,集合是size()方法,
for(int i = 0;i<o.length;i++) {
System.out.println(o[i]);
}
System.out.println(c);
}
}
运行结果