所以我想寫遞歸方法indexOf
它返回第一個字符串中第一個字符串的第一次出現的起始索引(如果未找到,則返回-1)。例如,indexOf(「Barack Obama」,「bam」)的調用將返回8.另外我知道字符串類有方法IndexOf,但我不想使用它。執行我自己的遞歸版本的indexOf方法
到目前爲止,這是我的代碼:
public class MyClass {
public static void main(String[] args) {
}
public static int indexOf(String s, String t) {
return abc(s, t, 0);
}
public static int abc(String a, String b, int c) {
if ((a.length() - c) < b.length()) {
return -1;
} else if (b.equals(a.substring(c, c + 3))) {
return c;
} else {
}
}
}
而你的問題是? – RealSkeptic
你的代碼還沒有遞歸。 – Codebender
另外,請給方法和變量有意義的名字。你的代碼不太可讀。不要使用's','t','a','b','c',但是可以解釋這些是什麼。一種方法不應該是'abc' - 它的名字應該解釋它的作用。 – RealSkeptic