Java源碼中有意思的代碼片斷

無聊時,看了一下java.util.Collections源碼,發現一個有趣的代碼片斷。下面這段代碼是從java.util.Collections中Copy出來的,大看一下有趣在哪裏??java

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
}

上面有個語法這樣的:

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;
        }

這樣的語法不多見,能夠說幾乎在Java根本沒有,一開始我還以是反編譯時編譯出錯了,出現了語法錯誤,可是我好奇的試了一下這個語法,真是奇了,居然能夠經過編譯,程序也能正常運行。dom

下面是個人代碼:

public class Test {
    
    public static void main(String[] args) {
        
        isNext : for (int i = 0; i < 5; i++) {
                if ((i % 2) != 0) {
                    continue isNext;
                }
                System.out.println("i ==> " + i);
            }
    }
}

運行結果:

clipboard.png

程序不沒有報錯,能夠正常運行,運行的結果和咱們使用標準的 for 循環同樣。從上面的運行結果能夠看出 continue isNext 的做用和 continue 同樣,都是跳出當前循環。ide

說道這裏,咱們分析同樣一下這個語言的使用場景,先看一下語法。spa

identifies :  
    code segment {
        break/continue identifies;
    }

identifies 是自定義的,定義的規則個Java的參數定義同樣,若是冒號後面跟的的循環,能夠使用 break identifies 跳出整個循環,或 continue identifies 退出當前循環。其實最上面的源碼中,就有註解: // Element mismatch, try next cand(元素不匹配,嘗試下一個元素),固然,我還發現另外一種用法,那就是,冒號後面跟着方法,也能夠執行方法。3d


eg: for(;;;) {continue identifies}

public static void isNext1() {
    // for(;;;) {continue identifies}
    isNext1 : for (int i = 0; i < 5; i++) {
        if ((i % 2) != 0) {
            continue isNext1;
        }
        System.out.println("i ==> " + i);
    }
}

clipboard.png

eg: do...while() {break identifies}

public static void isNext2() {
    // do...while() {break identifies}
    isNext2 : do {
        System.err.println("while() {break identifies}");
        if (true) {
            break isNext2;
        }
    } while (true);
}

clipboard.png

eg: do...while() {break identifies}

public static void isNext3() {
    // while() {break identifies}    
    isNext3 : while (true) {
        System.err.println("do...while() {break identifies}");
        if (true) {
            break isNext3;
        }
    }
}

clipboard.png

eg: switch() {break identifies}

public static void isNext4() {
    // switch() {break identifies}
    isNext4 : switch (1) {
        case 1:
            System.err.println("switch() {break identifies}");
            break isNext4;
        default:
            break isNext4;
    }
}

clipboard.png

eg: method()

public static void isNext5() {
    isNext : test(11);
}

public static void test(int arg) {
    System.err.println("method() ==> " + arg);
}

clipboard.png

總結

這種語法就是用來裝逼的,沒有什麼軟用。O(∩_∩)O哈哈哈~code

相關文章
相關標籤/搜索