Wednesday, April 22, 2009

List接口

java.util.List
List接口是java.util.Collection接口的子接口
Collection接口是 java.lang.Iterable的子接口
Iterable接口要求实现一个iterator()方法
...
public interface Iterable{
Iterator iterator();
}

Iterable 接口要求实现它的类返回一个实现java.util.Iterator接口的对象
但 实际上 API中不存在实现Iterator的类

Iterator接口的定义:
...
public interface Iterator{
boolean hasNext();
E next();
void remove();
}

Collection 接口继承了Iterator接口

...
interface Collection extends Iterable{
int size();
boolean isEmpty();
boolean contains(Object 0);
Iterator iterator();
T[] toArray(Collection c);
boolean add(E o);
boolean remove(Object o);
boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);
void clear();
boolean equals(Object o);
int hashCode();
}

Collection在删除元素及取得元素上的定义比较通用,List接口又增加了根据索引取得对象的方法,这说明了
List数据结构的特性:每个加入List中的元素是顺序加入的,并可指定索引来存取元素
List可以使用Array或Linked List来实现这个功能

0 comments:

Post a Comment