여기에 메서드 lastIndexOf
이 있고, 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
제안하시기 바랍니다.
str.lastIndexOf(ch)
이 숙제가 있습니다 .. 기능 접근 방식을 복용에 대해? – user1329572
및 기존 방법의 문제점 http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#lastIndexOf%28int%29? –
네, 제안이 필요합니다. – Hank