Collection中常用的方法:
int size(); 返回此collection中的元素个数。
boolean isEmpty(); 判断此collection中是否包含元素。
boolean contains(Object obj); 判断此collection是否包含指定(equals为true)的元素。
boolean containsAll(Collection c); 判断此collection是否包含指定 Collection c 中的所有元素。比较方式通过 equals比较boolean add(Object element); 向此collection中添加元素。
boolean addAll(Collection c); 将指定c中的所有元素添加到此collection中
boolean remove(Object element); 从此collection中移除指定的元素,如果有多个对象重复,则只会删除第一个。
boolean removeAll(Collection c); 移除此collection中那些也包含在指定 Collection c 中的所有元素。比较方式通过 equals比较
void clear(); 移除些collection中所有的元素。
boolean retainAll(Collection c); 仅保留此collection中那些也包含在指定 Collection c 的元素。比较方式通过 equals比较
Iterator iterator(); 返回在此collection的元素上进行迭代的迭代器。
Object[] toArray(); 把此collection转成数组。
List接口比Collection接口中新增的几个实用方法:
1. public E get(int index);返回列表中的指定位置的的元素
2. public void add(int index, E element); 在列表的指定位置插入指定元素。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动。
3. public E set(int index, E element) ; 用指定元素替换列表中指定位置的元素,返回被替换的元素
4. public E remove(int index) 移除列表中指定位置的元素
5. public ListIterator listIterator() 返回此列表元素的列表迭代器(List专有)
注意:list集合中的元素的索引与数组中元素的索引一样,均是从0开始。