2013-01-04 37 views
1

我在Java API Collection類中遇到了這個代碼。它是否像開關語句一樣工作?這個成語怎麼叫?Collection類中的奇怪控制語句

public static int indexOfSubList(List<?> source, List<?> target) { 
    int sourceSize = source.size(); 
    int targetSize = target.size(); 
    int maxCandidate = sourceSize - targetSize; 

    if (sourceSize < INDEXOFSUBLIST_THRESHOLD || 
     (source instanceof RandomAccess&&target instanceof RandomAccess)) { 
    nextCand: 
     for (int candidate = 0; candidate <= maxCandidate; candidate++) { 
      for (int i=0, j=candidate; i<targetSize; i++, j++) 
       if (!eq(target.get(i), source.get(j))) 
        continue nextCand; // Element mismatch, try next cand 
      return candidate; // All elements of candidate matched target 
     } 
    } else { // Iterator version of above algorithm 
     ListIterator<?> si = source.listIterator(); 
    nextCand: 
     for (int candidate = 0; candidate <= maxCandidate; candidate++) { 
      ListIterator<?> ti = target.listIterator(); 
      for (int i=0; i<targetSize; i++) { 
       if (!eq(ti.next(), si.next())) { 
        // Back up source iterator to next candidate 
        for (int j=0; j<i; j++) 
         si.previous(); 
        continue nextCand; 
       } 
      } 
      return candidate; 
     } 
    } 
    return -1; // No candidate matched the target 
} 
+2

是否*什麼*工作像切換?你已經提交了很多代碼。你真的只是對帶標籤的繼續語句感興趣嗎? –

+0

是的,我第一次看到它。 – jellyfication

+0

@JonSkeet我認爲他指的是標籤並繼續標註 –

回答

5

不,它只是一個標籤休息/繼續。在這裏看到:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Java允許使用標籤作爲中斷/繼續目標。默認情況下,break/continue會影響最內層循環,但使用標籤可以跳出外層循環。

+0

謝謝我從來沒有遇到過這個 – jellyfication

+0

它很少用,並且讓一些人交叉自己並稱你爲使用gotos的魔鬼:-) – radai

+0

標點符號!大寫! – Mob

1

假設你指的是nextCand:continue nextCand;,這是一個簡單的辦法繼續在循環的下一次迭代從循環中。

一個簡單的continue會繼續代替內部循環。