首页 > 其他 > 详细

Collection与List

时间:2015-04-23 12:27:30      阅读:210      评论:0      收藏:0      [点我收藏+]

集合接口

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开销大,除非非常接近表的端点。

 

Collection与List

原文:http://www.cnblogs.com/chuiyuan/p/4449941.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!