java的indexof实现了什么类型的算法



我需要弄清楚java.lang.StringindexOf()方法在其源代码中实现了哪种算法:http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java/?v=source

public int indexOf(int ch, int fromIndex) {
        int max = offset + count;
        char v[] = value;
        if (fromIndex < 0) {
            fromIndex = 0;
        } else if (fromIndex >= count) {
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }
        int i = offset + fromIndex;
        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
            // handle most cases here (ch is a BMP code point or a
            // negative value (invalid code point))
            for (; i < max ; i++) {
                if (v[i] == ch) {
                    return i - offset;
                }
            }
            return -1;
        }
        if (ch <= Character.MAX_CODE_POINT) {
            // handle supplementary characters here
            char[] surrogates = Character.toChars(ch);
            for (; i < max; i++) {
                if (v[i] == surrogates[0]) {
                    if (i + 1 == max) {
                        break;
                    }
                    if (v[i+1] == surrogates[1]) {
                        return i - offset;
                    }
                }
            }
        }
        return -1;
    }

据我所知,这应该是一个蛮力,没有高德纳-莫里斯-普拉特算法,因为没有强调寻找模式不匹配。

ode 使用蛮力算法在给定字符串中查找substring。也就是说,该方法以 O(mn) 运行,其中 m and n are the length of the source and target strings .

更多信息在这里和这里

最新更新