鏈接:http://blog.csdn.net/wangkr111/article/details/7884322
按鏈表的組織形式分有ArrayList和LinkList兩種。ArrayList內部其實是用數組的形式實現鏈表,比較適合鏈表大小確定或較少對鏈表進行增刪操作的情況,同時對每個鏈表節點的訪問時間都是constant;而LinkList內部以一個List實現鏈表,比較適合需要頻繁對鏈表進行操作的情況,對鏈表節點的訪問時間與鏈表長度有關O(N)。
另外,根據實現形式可以分爲直接式(想不出什麼合適的名字,姑且這樣吧)和使用Iterator(迭代模式)兩種方法。直接式的實現方法和C/C++中的寫法差不多;而使用Iterator時,需要實現java.lan中的Iterable接口(或者也可以自己在鏈表內部定義自己的Iterator method)
關於直接實現鏈表好,還是使用迭代模式好這個問題,有兩個帖子可以看看:
1. http://topic.csdn.net/t/20050722/17/4162226.html這篇帖子,雖然短小,但也說出了一些使用迭代模式設計鏈表的好處;
2.後來又看到了http://www.java63.com/design_pattern/iterator_pattern.html這篇帖子,覺得說的比較清楚了。
我這裏再撿主要的說一下:
使用迭代模式的優點:
1,實現功能分離,簡化容器接口。讓容器只實現本身的基本功能,把迭代功能委讓給外部類實現,符合類的設計原則。
2,隱藏容器的實現細節。
3,爲容器或其子容器提供了一個統一接口,一方面方便調用;另一方面使得調用者不必關注迭代器的實現細節。
4,可以爲容器或其子容器實現不同的迭代方法或多個迭代方法。
我覺得第4點說的很好,對於一堆數據而言,不同的人(或業務邏輯)使用它的方式也不盡相同,定義一個theIterator繼承Iterator,不僅提供next,hasNext 以及remove這個最小的操作集合,同時也可以提供更多的其它方法。在theIterator的實現類中,具體實現不同的迭代方法,比如順序、逆序或根據其它語義進行遍歷等,再通過類廠的方式將一個theIterator實現的對象交給用戶使用。
下面我給出兩個例子:
首先是直接式實現鏈表的例子,這個例子只提供了幾種鏈表操作的基本方法,僅用於示意:
publicclass MyList<AnyType> {
privateclass Node<AnyType>{
public Node<AnyType> pre;
public Node<AnyType> next;
public AnyType data;
public Node(AnyType d, Node<AnyType>p, Node<AnyType> n){}
public Node(){}
}
privateint theSize;
private Node<AnyType> Header;
private Node<AnyType> Tail;
public MyList(){}
publicvoid add(AnyType item){}
publicboolean isEmpty(){}
publicint size(){}
public AnyType get( int idx){}
publicvoid print(){}
}
Node<AnyType>類定義了雙向鏈表中節點的結構,它是一個私有類,而其屬性和構造函數都是公有的,這樣,其父類可以直接訪問其屬性,而外部類根本不知道Node類的存在。Data是節點中的數據與,pre指向前一個Node節點,next指向後一個Node節點。其構造函數的實現如下,不解釋:
下面我們看一下鏈表的構造函數實現:
我們構造了一個帶有頭、尾節點的雙向鏈表,頭節點的Next指向尾節點,爲節點的pre指向頭節點。鏈表長度起始爲0。
繼續貼上鍊表類其它方法的實現,不解釋了,應該比較清楚:
publicvoid add(AnyType item){
Node<AnyType> aNode = new Node<AnyType>(item,null,null);
Tail.pre.next = aNode;
aNode.pre = Tail.pre;
aNode.next = Tail;
Tail.pre = aNode;
theSize++;
}
publicboolean isEmpty(){
return ( theSize == 0);
}
publicint size(){
return theSize;
}
public AnyType get( int idx){
if(idx > theSize-1 || idx < 0)
thrownew IndexOutOfBoundsException();
Node<AnyType> current = new Node<AnyType>(null,Header,null);
for(int i = 0; i<idx; i++)
current = current.next;
return current.data;
}
publicvoid print(){
Node<AnyType> current = Header.next;
while(current.next != null){
//如果AnyType是你自己定義 //的數據類型,那麼請務必提供
//一個toString方法,要麼就不
//要在鏈表裏實現print方法。
System.out.println(current.data.toString());
current = current.next;
}
}
第二個例子是用迭代方法實現鏈表的例子,下面是類定義:
publicclass MyListItr<Type> implements Iterable<Type> {
privateclass Node<Type>{
public Type data;
public Node<Type> pre;
public Node<Type> next;
public Node(){}
public Node(Type d, Node<Type> p, Node<Type> n){}
}
private Node<Type> Header;
private Node<Type> Tail;
privateint theSize;
public MyListItr(){}
publicvoid add(Type item){}
publicvoid print(){}
publicint size(){}
@Override
public Iterator<Type> iterator() {}
privateclass MyListIterator implements Iterator<Type>{
@Override
publicboolean hasNext() {}
@Override
public Type next() {}
@Override
publicvoid remove() {}
}
}
這裏主要說一下與前面例子不同的地方:MyListItr類實現了java.lan.Itrable接口,因此我們必須實現其public Iterator<Type> iterator() {}方法,它的返回值是一個迭代器對象,那麼我就定義一個內部私有類——MyListIterator,這個類實現了iterator接口。在public Iterator<Type> iterator() 方法中,我就構造這麼一個MyListIterator的對象並返回。
Iterator接口有三個方法是我們必須實現的,hasNext,next和remove,這是迭代操作的最小集合了。對於不同的迭代器執行方式,我們可以定義多個類似MyListIterator這樣的實現類,只要在鏈表類中的iterator() 方法中把具體實現類的對象返回給用戶就OK了。
羅嗦兩句:我感覺如果我們定義鏈表類時,如果不繼承Iterable接口,而是直接在類內部提供 public theIterator Iterator() 方法,theIterator是一個自定義接口,繼承自Iterator接口,這樣我們可以提供遵循theIterator接口的各種迭代方式的不同實現類,並通過一個類廠方法在鏈表的public theIterator Iterator() 方法中把具體迭代實現類的對象交給用戶,以此就可以根據不同的需求,提供鏈表的多種迭代方式。我覺得這種方法是可行的,但並沒有具體實驗。
下面只貼出鏈表iterator()方法和迭代實現類的MyListIterator代碼,其它方法就不重複貼了:
@Override
public Iterator<Type> iterator() {
returnnew MyListIterator();
}
privateclass MyListIterator implements Iterator<Type>{
Node<Type> current = Header.next;
@Override
publicboolean hasNext() {
return (current != Tail);
}
@Override
public Type next() {
if(!hasNext())
thrownew IndexOutOfBoundsException();
Type item = current.data;
current = current.next;
return item;
}
@Override
publicvoid remove() {
if( !hasNext())
thrownew NoSuchElementException();
current.pre.next = current.next;
current.next.pre = current.pre;
current = current.next;
theSize--;
}
}
測試代碼:
publicclass MyListTest {
/**
* @param args
*/
publicstaticvoid main(String[] args) {
MyList<Integer> intList = new MyList<Integer>();
for (int i = 0; i < 10; i++) {
intList.add(i);
}
intList.print();
MyListItr<ItemInfo> itrList = new MyListItr<ItemInfo>();
for (int i = 0; i < 10; i++) {
ItemInfo aItem = new ItemInfo("name_" + i, "score_" + i);
itrList.add(aItem);
}
itrList.print();
System.out.println("=======print using iterator type_1==========");
Iterator<ItemInfo> Itor = itrList.iterator();
for (ItemInfo str : itrList)
System.out.println(str);
System.out.println("=======print using iterator type_2==========");
while(Itor.hasNext())
System.out.println(Itor.next().toString());
System.out.println("-------------------after remove----------------");
while (Itor.hasNext())
Itor.remove();
while(Itor.hasNext())
System.out.println(Itor.next().toString());
}
}
其中 ItemInfo的定義如下: