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;
}
}
本來是想畫圖的,但時間較緊,向每一個打算讀的朋友抱歉。歡迎交流,歡迎批評指正!