Java Iterator ListIterator 理解


一、 Iterator 常用操作 next hasNext remove  

先上源碼:JDK8 簡化版本,用於說明問題

 1 private class Itr implements Iterator<E> {
 2     int cursor;       // index of next element to return
 3     int lastRet = -1; // index of last element returned; -1 if no such
 4 
 5     public boolean hasNext() {
 6         return cursor != size;
 7     }
 8     
 9     public E next() {
10         int i = cursor;
11         Object[] elementData = ArrayList.this.elementData;
12         cursor = i + 1;
13         return (E) elementData[lastRet = i];
14     }
15 
16     public void remove() {
17         ArrayList.this.remove(lastRet);
18         cursor = lastRet;
19         lastRet = -1;
20     }
21     
22 }

約定: Iterator  it = xxx.iterator(); 

疑惑1: 為什么不像 c++ 中 iterator 直接 *it 就可以獲取當前值,java必須要 Object obj = it.next();

答:查看源碼發現 調用 next 函數有兩個作用,一個是獲取當前 cusor 指向位置的元素,另一個是指針cusor 后移,並且 賦值 lastRet 變量(這里很關鍵)

疑惑二:為什么數組的最后一個元素進行 hasNext 返回真?

答:在迭代器中 可訪問范圍為 [0, size] 不是 [0, size)。因此傳統意義數組的最后一個元素的下一個是 array[size], 盡管這個是無意義的,但是在迭代器中算一個特殊迭代器(類似 C++ . iterator.end() )

疑惑三:為什么刪除元素不可以這么寫?

1 while(it.hasNext()){
2   it.remove();
3   it.next();
4 }

答:查看疑問一答中,next函數會賦值變量 lastRet ,這個變量對於 remove 函數相當重要。 因此在首次進入 while 循環的時候, laseRet = -1 (默認狀態),因此不能直接進行 remove ,數組越界。

所以正確的應該是

1 while(it.hasNext()){
2     it.next();
3     it.remove();
4 }

 

二、 ListIterator 中 lastIndex previousIndex previous

先上源碼:JDK8 簡化版本,用於說明問題

 1      public boolean hasPrevious() {
 2             return cursor != 0;
 3         }
 4 
 5         public int nextIndex() {
 6             return cursor;
 7         }
 8 
 9         public int previousIndex() {
10             return cursor - 1;
11         }
12 
13         public E previous() {
14             int i = cursor - 1;
15             Object[] elementData = ArrayList.this.elementData;
16             cursor = i;
17             return (E) elementData[lastRet = i];
18         }

疑惑一: 如何獲取正確的 前一個元素的下標值和后一個元素的下標值?

答:先看一段代碼:

1 public static void main(String[] args) {
2     List<Integer> list = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 7, 8));
3     ListIterator<Integer> it = list.listIterator();
4     System.out.println(it.previousIndex());
5     System.out.println(it.nextIndex());
6 }
//output
-1
0

顯然輸出並不正確,我們期望得到的是 -1 1

解釋這個現象看源碼,我們發現 

① previousIndex 和 nextIndex 只能使用在后向移動的迭代器中,盡管ListIterator 是一個雙向迭代器

② previousIndex 和 nextIndex 返回值依賴於 cursor 當前的數值,因此 上面代碼中 cursor=0,所以 得出錯誤結果。要想獲取下一個索引的正確之,我們需要 調用一次 next 函數幫助我們調整 cursor

這樣即可獲取我們期望的數值:

 1 public static void main(String[] args) {
 2     List<Integer> list = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 7, 8));
 3     ListIterator<Integer> it = list.listIterator();
 4     System.out.println(it.previousIndex());
 5     it.next();
 6     System.out.println(it.nextIndex());
 7 }
//output 
-1
1

 

注: previous 和 next 函數中 cusor 移動不一樣,

next函數是獲取了當前值但是 cursor 已經移動到了下一個,相當於 return array[cursor++];

previous 函數是移動到前一個並且獲取值   ,相當於 return array[--cursor];

 

 

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM