【发布时间】:2015-08-03 12:09:36
【问题描述】:
所以我正在尝试编写 recursive 方法indexOf,它返回第一个字符串中第二个字符串第一次出现的起始索引(如果没有找到,则返回-1)。例如, indexOf ("Barack Obama", "bam") 的调用将返回 8。另外我知道 String 类有方法 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 {
}
}
}
【问题讨论】:
-
你的问题是?
-
您的代码中还没有递归。
-
另外,请给方法和变量起有意义的名字。您的代码不是很可读。不要使用
s,t,a,b,c,而是使用解释这些内容的名称。一个方法不应该是abc- 它的名字应该解释它的作用。
标签: java string recursion indexof