String.class中的indexOf()方法为什么代码要这样写?
Why does code write this way the method indexOf() in String.class?
我正在阅读 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:If sourceCount
表示整个字符串的长度,为什么不用source.length
* 反而 ?
*/
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
这里说的是在搜索索引(fromIndex
)结束后,字符串长度为(sourceCount
),然后是return -1
(没有找到)除非寻找的字符串是 ""
- "always found." 非常具体。
此方法接受两个字符数组 - source
数组是要搜索的数组,target
数组是要搜索的数组。
但是,offset 和 count 变量将搜索限制为 source
的子数组和 target
的子数组。
基本上,您是在从 source[sourceOffSet]
到 source[sourceOffset+sourceCount-1]
的子数组中搜索由 target[targetOffSet]
到 [ 的子数组中的字符组成的 String
=21=].
这是一个例子。搜索的相关数组是子数组:
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
数组)。
我正在阅读 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:If sourceCount
表示整个字符串的长度,为什么不用source.length
* 反而 ?
*/
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
这里说的是在搜索索引(fromIndex
)结束后,字符串长度为(sourceCount
),然后是return -1
(没有找到)除非寻找的字符串是 ""
- "always found." 非常具体。
此方法接受两个字符数组 - source
数组是要搜索的数组,target
数组是要搜索的数组。
但是,offset 和 count 变量将搜索限制为 source
的子数组和 target
的子数组。
基本上,您是在从 source[sourceOffSet]
到 source[sourceOffset+sourceCount-1]
的子数组中搜索由 target[targetOffSet]
到 [ 的子数组中的字符组成的 String
=21=].
这是一个例子。搜索的相关数组是子数组:
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
数组)。