2013-01-04 32 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会继续代替内部循环。