Collection接口
- Collection:该体系结构的根为接口,代表一组对象,称为“集合”。
- List接口的特点:有序、有下标、元素可重复。
- Set接口的特点:无序、无下标、元素不能重复。
Collection父接口
-
特点:代表一组任意类型的对象,无序、无下标、不能重复。。
-
方法:
- boolean add(Object obj) 添加一个对象。
- boolean addAll(Collection c) 将一个集合中的所有对象添加到此集合中。
- void clear() 清空此集合中的所有对象。
- boolean contains(Object o) 检查此集合中是否包含o对象。
- boolean equals(Object o) 比较此集合是否与指定对象想等。
- boolean isEmpty() 判断此集合是否为空。
- boolean remove(Object o) 在此集合中移除o对象
- int size() 返回此集合中的元素个数。
- Object[] toArray() 将此集合转换成数组。
-
实例:
public class Collectiontest { public static void main(String[] args) { Collection collection = new ArrayList(); // 添加集合 System.out.println("----------添加集合-----------"); collection.add("苹果"); collection.add("西瓜"); collection.add("榴莲"); System.out.println("元素个数:"+collection.size()); System.out.println(collection.toString()); // 删除元素 System.out.println("----------删除元素-----------"); collection.remove("榴莲"); System