[英]Passing return value of method as parameter to another method in same class
Is it possible to use return value(i) from find method as the parameter of isSubstringAt as(index)? 是否可以使用find方法的返回值(i)作为isSubstringAt as(index)的参数?
if not possible : 如果不可能的话:
how can I assign (index) value to (i) value in isSubstringAt Method? 如何在isSubstringAt方法中将(索引)值分配给(i)值?
Thanks in advance! 提前致谢!
class Finder {
public int find(String orginalString, String subString) {
int m = subString.length();
int n = orginalString.length();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (orginalString.charAt(i + j) != subString.charAt(j)) {
break;
}
}
if (j == m) {
return i;
}
}
return -1;
}
public boolean isSubstringAt(String originalString, String substring, int index) {
if (index + substring.length() > originalString.length()) {
return false;
}
for (int j = 0; j < substring.length(); j++) {
if (originalString.charAt(index + j) != substring.charAt(j)) {
return false;
}
}
return true;
}
}
Wherever you call find, assign the value to a variable and pass it into the next method: 无论您在哪里调用find,都将值分配给变量并将其传递给下一个方法:
int index = find(original, sub);
boolean isSub = isSubstringAt(original, sub, index);
is this the desired result? 这是理想的结果吗?
this can be done on one line but using variables for easy reading
可以一行完成,但使用变量易于阅读
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.