有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么代码以这种方式编写字符串中的indexOf()方法。班

我正在读String.class的源代码
在方法indexOf()中,我看到了一些我无法理解的东西
下面是来自String.class源代码的indexOf()代码片段

/**
 * Code shared by String and StringBuffer to do searches. The
 * source is the character array being searched, and the target
 * is the string being searched for.
 *
 * @param   source       the characters being searched.
 * @param   sourceOffset offset of the source string.
 * @param   sourceCount  count of the source string.
 * @param   target       the characters being searched for.
 * @param   targetOffset offset of the target string.
 * @param   targetCount  count of the target string.
 * @param   fromIndex    the index to begin searching from.
 */
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;
}

我无法理解这里的代码

if (fromIndex >= sourceCount) {
    return (targetCount == 0 ? sourceCount : -1);
}

如果source String"abcdedefg"sourceOffset2sourceCount3, 我想从中搜索“d”, 为什么我不能从索引4中搜索
/**
*Ps:如果sourceCount表示整个字符串的长度,为什么不使用source.length
*相反
*/


共 (2) 个答案

  1. # 1 楼答案

    此方法接受两个字符数组source数组是正在搜索的数组target数组是正在搜索的数组

    然而,偏移量和计数变量将搜索限制为source的子数组和target的子数组

    基本上,您是在从source[sourceOffSet]source[sourceOffset+sourceCount-1]的子数组中搜索由从target[targetOffSet]target[targetOffset+targetCount-1]的子数组中的字符组成的String

    这是一个例子。搜索的相关数组为子数组:

    source array : |          |
    sub array    :       |   -|        
                       source  source
                       offset  offset +
                               source
                               count - 1
    
    target array : |          |
    sub array    :       |   -|        
                       target  target
                       offset  offset +
                               target
                               count - 1
    

    然而,通过提供fromIndex进一步限制了搜索。从source子数组的第fromIndex个索引开始搜索

    由于source子数组的长度为sourceCount,如果fromIndex >= sourceCount,则无法找到target子数组,因此除非target子数组为空(即targetCount == 0),否则返回-1

    让我们考虑一下你的例子:

    source : "abcdedefg"
    sourceOffset : 2
    sourceCount : 3
    target : "d"
    targetOffset : 0
    targetCount : 1
    fromIndex : 4
    

    这些参数意味着您正在源子字符串"cde"中搜索从索引4开始的目标子字符串"d"。但是,在"cde"中没有索引4,因此返回-1

    关于你的

    Ps:If the sourceCount means the length of the whole string, why not use source.length instead

    正如我所解释的sourceCount并不意味着整个source数组的长度,只是被搜索的子数组的长度

    请注意,当您调用someString.indexOf(str,fromIndex)时,您询问的static方法将使用以下参数进行调用:

    public int indexOf(String str, int fromIndex) {
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }
    

    在这种情况下sourceCount等于source.length(即从fromIndex开始搜索整个source数组)

  2. # 2 楼答案

        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
    

    这意味着在搜索索引(fromIndex)中,字符串长度(sourceCount)在末尾之后,然后返回-1(未找到),除非搜索的字符串是"",这是“始终找到的”非常具体