これが methodlastIndexOf
にあるもので、ch
は一致する文字、str
はソース文字列です。
public static int lastIndexOf(char ch, String str) {
// check for null string or empty string
if (str.length() == 0 || str == null) {
return -1;
}
int indexInRest = lastIndexOf(ch, str.substring(1));
char first = str.charAt(0);
// recursive call to find the last matching character
if (first == ch) {
return 1 + indexInRest; // this might not work properly
} else
return indexInRest;
}
私のクラスのメインメソッドで私が呼び出す場合:
System.out.println(lastIndexOf('r', "recurse"));
System.out.println(lastIndexOf('p', "recurse"));
私は得た:
1
-1
望ましい結果は次のとおりです。
4
-1
提案してください。