Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jdk中字符串类的indexOf方法是使用BF实现的,为什么不使用KMP或BM呢?_Java_String_Indexof - Fatal编程技术网

Java jdk中字符串类的indexOf方法是使用BF实现的,为什么不使用KMP或BM呢?

Java jdk中字符串类的indexOf方法是使用BF实现的,为什么不使用KMP或BM呢?,java,string,indexof,Java,String,Indexof,jdk中字符串类的indexOf方法是使用BF实现的,为什么不使用KMP或BM呢? 下面是jdk如何实现此功能的。它使用BF来解决。为什么不使用更有效的方法,如KMP、BM static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int

jdk中字符串类的indexOf方法是使用BF实现的,为什么不使用KMP或BM呢? 下面是jdk如何实现此功能的。它使用BF来解决。为什么不使用更有效的方法,如KMP、BM

   static int indexOf(char[] source, int sourceOffset, int sourceCount,
                   char[] target, int targetOffset, int targetCount,
                   int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first  = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                 while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j] ==
                     target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
   }
static int indexOf(char[]source,int sourceOffset,int sourceCount,
char[]目标,int targetOffset,int targetCount,
int fromIndex){
if(fromIndex>=sourceCount){
返回(targetCount==0?sourceCount:-1);
}
如果(从索引<0){
fromIndex=0;
}
如果(targetCount==0){
从索引返回;
}
char first=目标[targetOffset];
int max=sourceOffset+(sourceCount-targetCount);
对于(int i=sourceOffset+fromIndex;i)
为什么不使用更有效的方法,如KMP、BM

   static int indexOf(char[] source, int sourceOffset, int sourceCount,
                   char[] target, int targetOffset, int targetCount,
                   int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first  = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                 while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j] ==
                     target[k]; j++, k++);

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
   }
更高级的字符串搜索算法有一个不平凡的设置时间。如果您正在执行一次性字符串搜索,涉及的目标字符串不太大,您会发现在设置上花费的时间比在字符串搜索期间节省的时间要多

即使只是测试目标和搜索字符串的长度也不能很好地回答使用高级算法是否“值得”。你从(比如)Boyer Moore那里得到的实际加速取决于字符串的值,即字符模式

Java实现者采取了务实的方法。他们不能保证高级算法能够提供更好的性能,无论是平均性能还是特定的输入性能。因此,他们将其留给程序员来处理……必要时



FWIW,我不知道有任何其他主流编程语言在其运行时库的标准“字符串查找”功能中使用BM等。

BM和KMP的可能副本具有增加启动开销的预处理要求,因此它们并不总是“更高效”.因为他们就是这样实施的。你得问问作者。