String中的index
String中的索引有indexOf、lastIndexOf,主要对字符、字符串进行索引。此处主要想写lastIndexOf(String str, int fromIndex)。首先看一段小代码。此处对应的方法为lastIndexOf(String str, int fromIndex).
public class StringIndexTest {
public static void main(String args[]) {
String source = "DuJingDuJingDu";
String target = "Jing";
System.out.println("source string is : " + source + "\n" + "target string is : " + target);
System.out.println("source.lastIndexOf(target, 1) : " + source.lastIndexOf(target, 1));
System.out.println("source.lastIndexOf(target, 2) : " + source.lastIndexOf(target, 2));
System.out.println("source.lastIndexOf(target, 6) : " + source.lastIndexOf(target, 6));
}
}
/**
* Result
* source string is : DuJingDuJingDu
* target string is : Jing
* source.lastIndexOf(target, 1) : -1
* source.lastIndexOf(target, 2) : 2
* source.lastIndexOf(target, 6) : 2
*/
从代码可得出以下几点:
第1:fromIndex,即给出的索引值是倒序的索引值,lastIndexOf是采取例序的方法检索的;
第2:案例中看出,案例中source.lastIndexOf(target,2),fromIndex是2,此处与target仅仅对应了一个字符'J',竟然也能索引成功。可知fromIndex只是target首字符的索引起始值。
接下来,从源码角度分析。lastIndexOf(String str)和lastIndexOf(String str, int fromIndex)全部引用下边的方法,所以直入正题。
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex)
从源码一句句来看:
/**
* 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 lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
/*
* Check arguments; return immediately where possible. For
* consistency, don't check for null str.
*/
//采取倒序的方式检索,fromIndex为target首字符的倒序检索起始位置;
//因此,若fromIndex对应字符与target首字符相同,但target并不在整个检索域中,也能检索成功.
int rightIndex = sourceCount - targetCount;//计算出最大可能性的阈值
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;//当人为设置的fromIndex超出阈值时,自动进行调整.
}
/* Empty string always matches. */
//目标字符串为空,默认检索成功,返回检索起始位置fromIndex.
if (targetCount == 0) {
return fromIndex;
}
int strLastIndex = targetOffset + targetCount - 1;//得出target的最后索引.
char strLastChar = target[strLastIndex];
//以最右端对应的索引值,min表示最小索引值,i为最大索引值.
int min = sourceOffset + targetCount - 1;
int i = min + fromIndex;
startSearchForLastChar:
while (true) {
while (i >= min && source[i] != strLastChar) {
i--;//找到最后一个字符相等的位置.
}
if (i < min) {
return -1;
}
//设置起始、终止位置,对两个字符串进行比较.
int j = i - 1;
int start = j - (targetCount - 1);
int k = strLastIndex - 1;
while (j > start) {
if (source[j--] != target[k--]) {
i--;//当存在一个字符不相等的情况下,开始进入下一轮循环.
continue startSearchForLastChar;
}
}
//当退出循环走到这一步时,表明每个字符都相等,所以返回具体位置.
return start - sourceOffset + 1;
}
}
本来是想画图的,但时间较紧,向每一个打算读的朋友抱歉。欢迎交流,欢迎批评指正!