简体   繁体   English

将方法的返回值作为参数传递给同一类中的另一个方法

[英]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.

相关问题 将值从布尔方法传递到同一类中的另一个方法 - passing value from a boolean method to another method in the same class Getters 方法在相同的 class 中返回值,但在另一个 class 中返回 0 - Getters method is return value in same class but return 0 in another class 将 Callable 作为参数传递给方法并使用其返回值作为该方法的参数 - Passing a Callable as parameter to a method and use its return value as parameter for that same method 将ArrayList从一个方法传递到同一类中的另一个方法? - Passing an ArrayList from a method to another in the same class? 从另一个类和方法返回一个类中的值 - Return value in a class from another class and method 从另一个类的方法获取参数值 - Get parameter value from method in another class 如何从另一个方法的返回方法中获取参数的值? - How to get parameter's value in a return method from another method? 如何将一个方法的返回值用作另一个方法的参数? - How can I use the return value of a method as a parameter in another method? 如何调用一个方法并将其返回值用作另一个方法参数? - How to call a method and use it's return value as an another method parameter? 如何将一个方法的返回值用作另一个方法的参数? - How could I apply the return value of a method as a parameter for another method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM