public class StringArray {
private String strArr[];
public StringArray(int capacity) {
strArr = new String [capacity];
}
public int indexOf(String s) throws StringNotFoundException {
for(int i=0;i<strArr.length ;++i) {
if (strArr[i].equals(s)) {
return i;
} else {
throw new StringNotFoundException();
}
}
}
}
我想要做的是返回我正在寻找的字符串的索引,如果它在数组中,否则抛出异常。
但是 Eclipse 说我必须返回一个 int。
那么我应该将返回类型更改为 void 还是有其他选项?
StringNotFoundException 是我编造的一个自定义异常。