| NO | 方法名称 | 描述 |
| 1 | public boolean add(E e) | 向集合中保存数据 |
| 2 | public void clear() | 清空集合 |
| 3 | public boolean contains(Object o) | 查询集合之中是否有指定对象 ,该方法需要equals()方法支持 |
| 4 | containsAll(Collection<?> c) | |
| 5 | boolean remove(Object o) | |
| 6 | boolean removeAll(Collection<?> c) | |
| 7 | boolean isEmpty() | |
| 8 | int size() | |
| 9 | Object[] toArray() | |
| 10 | Iterator<E> iterator() | 为iterator接口实例化 |
在以上所给方法中使用最多的是add() 和 iterator()两个方法。其他方法使用较少。
开发中大部分不会去直接使用Collection 而是使用Collection下的两个接口:List(允许重复) Set(不允许重复)
- 允许重复的子接口:List(80%只用他)
- List对Collection所作的扩充主要有:
| NO | 方法名称 | 描述 |
| 1 | E get(int index) | 取得指定位置的对象 |
| 2 |
E set(int index,E element) |
|
| 3 | ListIterator<E> listIterator() | 为listIterator实例化 |
接口完成后一定要使用子类,而常用的两个子类是:ArrayList Vector
ArrayList使用(90%)
-
View Code
import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("hello"); list.add("hello"); list.add("world!"); System.out.println(list); } }