标签:
集合接口
import java.util.Iterator;
public interface Collection <AnyType> extends Iterable<AnyType>{
int size ();
boolean isEmpty();
void clear ();
boolean contains(AnyType x );
boolean add (AnyType x) ;
boolean remove(AnyType x) ;
Iterator<AnyType> iterable() ;
}
实现 Iterable的接口要提供一个iterator 的方法。
public interface Iterator<AnyType> {
boolean hasNext ();
AnyType next ();
void remove();
}
如果集合正在进行改变的时候,iterator的使用就不再合法。因此只有在要立即使用迭代器的时候才用,当然如果是
用的迭代器自己的remove,是没有问题的。相比于Collection的remove方法,collection的remove要先找出被删除的项才行,因此成本
可能 更高。
List接口子集
public interface List<AnyType> extends Collection<AnyType> {
AnyType get(int index) ;
AnyType set(int index, AnyType newVal) ;
void remove (int index );
ListIterator< AnyType> listIterator( int pos);
}
List的两种实现
1.ArrayList
可增长数组的实现。
优点 :get/set 常数时间。
缺点:inset/delete 代价比较大,除非是在末端进行的。
2. LinkedList
双链表实现。
优点 :insert/delete开销常数时间,这里是假设变动项的位置已知。
缺点:不容易做索引 ,get开销大,除非非常接近表的端点。
标签:
原文地址:http://www.cnblogs.com/chuiyuan/p/4449941.html