我正在阅读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”
,sourceOffset
为2
,sourceCount
是3
,
我想从中搜索“d”,
为什么我不能从索引 4 开始搜索?
/**
* Ps:如果sourceCount
表示整个字符串的长度,为什么不使用source.length
* 反而 ?
*/
最佳答案
此方法接受两个字符数组 - source
数组是正在搜索的数组,target
数组是正在搜索的数组。
但是,偏移量和计数变量将搜索限制为源
的子数组和目标
的子数组。
基本上,您是在从 source[sourceOffSet]
到 source[sourceOffset+sourceCount-1]
的子数组中搜索 String
由从 target[targetOffSet]
到 target[targetOffset+targetCount-1]
的子数组中的字符组成。
这是一个例子。用于搜索的相关数组是子数组:
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
数组)/
关于java - 为什么代码要这样写String.class中的方法indexOf()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345230/