Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归查找并行数组上的值_Java_Recursion - Fatal编程技术网

Java 递归查找并行数组上的值

Java 递归查找并行数组上的值,java,recursion,Java,Recursion,我需要编写一个递归方法,该方法使用两个并行数组和要查找的单词,查找指定的单词,并在每次索引与另一个数组匹配时求和。例如: array1=“丰田”、“本田”、“本田”、“丰田”、“丰田”、……n array2 = 22500, 18000, 29000, 22500, 32000, ....... n 如果我说我需要查找单词“Toyota”,它应该在找到索引时对第二个数组中的值求和。在这种情况下,其总和应为:22500+22500+32000 我如何使我的递归方法,使它采取适当的参数,使计算递归

我需要编写一个递归方法,该方法使用两个并行数组和要查找的单词,查找指定的单词,并在每次索引与另一个数组匹配时求和。例如:

array1=“丰田”、“本田”、“本田”、“丰田”、“丰田”、……n

array2 = 22500, 18000, 29000, 22500, 32000, ....... n
如果我说我需要查找单词
“Toyota”
,它应该在找到索引时对第二个数组中的值求和。在这种情况下,其总和应为:
22500+22500+32000

我如何使我的递归方法,使它采取适当的参数,使计算递归。我将使用硬编码值

这就是我目前所拥有的。我很确定我的递归方法需要更多的参数,但我会看看是否有人能帮我

static int printRecursively(int A[], int N) {
        if(N <= 0) {

            return 0;

        }

        return (printRecursively(A, N - 1) + A[N -1]);
    }

}
静态int递归打印(int A[],int N){

如果(N我认为您当前的数据结构不适合此问题。相反,我建议使用cars到值的hashmap:

Map<String, List<Integer>> map = new HashMap<>();
List<Integer> values = Arrays.asList(22500, 22500, 32000);
map.put("Toyota", values);
values = Arrays.asList(18000, 29000);
map.put("Honda", values);

一般来说,一个很好的方法是在汽车是钥匙的位置表示数据,并且值与钥匙指向的方向一致。

我认为您当前的数据结构不适合此问题。相反,我建议使用汽车到值的哈希映射:

Map<String, List<Integer>> map = new HashMap<>();
List<Integer> values = Arrays.asList(22500, 22500, 32000);
map.put("Toyota", values);
values = Arrays.asList(18000, 29000);
map.put("Honda", values);

一般来说,一个很好的方法是表示车钥匙所在的数据,这些值与钥匙所指的值相同。

类似的内容以下内容可能适合您的需要

public static int recursiveSum(String search, String[] names, int[] values, int start) {
    // Check that the two arrays are of the same length
    // And that start does not exceed the bounds of either
    if((names.length != values.length) || start > names.length)
        return 0;

    // If the value at the 'start' of the array is the one we're looking for
    if(names[start].equals(search)) {
        return values[start] + recursiveSum(search, names, values, start + 1);
    } else {
        // Otherwise just skip onto the next value of the arrays
        return recursiveSum(search, names, values, start + 1);
    }
}


recursiveSum("Toyota", names, values, 0)

下面类似的东西可能适合你的需要

public static int recursiveSum(String search, String[] names, int[] values, int start) {
    // Check that the two arrays are of the same length
    // And that start does not exceed the bounds of either
    if((names.length != values.length) || start > names.length)
        return 0;

    // If the value at the 'start' of the array is the one we're looking for
    if(names[start].equals(search)) {
        return values[start] + recursiveSum(search, names, values, start + 1);
    } else {
        // Otherwise just skip onto the next value of the arrays
        return recursiveSum(search, names, values, start + 1);
    }
}


recursiveSum("Toyota", names, values, 0)
从位置0处的“游标”开始。然后返回该位置处的数字的总和,以及从相同方法调用返回的任何总和,其游标值为
cursor+1
。如果
cursor+1
,则表示您已到达数组的末尾,在这种情况下,只需返回该位置处的数字即可

public static void main(String[] args) {
    String arr1[] = new String[]{"Toyota", "Honda", "Honda", "Toyota", "Toyota"};
    int arr2[] = new int[]{22500, 18000, 29000, 22500, 32000};

    System.out.println(getSum(arr1, arr2, "Toyota", 0));
}

private static int getSum(String arr1[], int arr2[], String word, int cursor) {
    if (cursor == arr1.length - 1) return arr1[arr1.length - 1].equals(word) ? arr2[arr2.length - 1] : 0;
    return arr1[cursor].equals(word)
            ? arr2[cursor] + getSum(arr1, arr2, word, cursor + 1)
            : getSum(arr1, arr2, word, cursor + 1);
}
输出

77000
从位置0处的“游标”开始。然后返回该位置处的数字的总和,以及从相同方法调用返回的任何总和,其游标值为
cursor+1
。如果
cursor+1
,则表示您已到达数组的末尾,在这种情况下,只需返回该位置处的数字即可

public static void main(String[] args) {
    String arr1[] = new String[]{"Toyota", "Honda", "Honda", "Toyota", "Toyota"};
    int arr2[] = new int[]{22500, 18000, 29000, 22500, 32000};

    System.out.println(getSum(arr1, arr2, "Toyota", 0));
}

private static int getSum(String arr1[], int arr2[], String word, int cursor) {
    if (cursor == arr1.length - 1) return arr1[arr1.length - 1].equals(word) ? arr2[arr2.length - 1] : 0;
    return arr1[cursor].equals(word)
            ? arr2[cursor] + getSum(arr1, arr2, word, cursor + 1)
            : getSum(arr1, arr2, word, cursor + 1);
}
输出

77000

如果你不传递
字符串[]
字符串
,你将如何“查找单词Toyota”?你为什么在这里传递
int N
?为什么需要递归?已经问过了?你要小心,这可能会导致你如何“查找单词Toyota”如果未传递
字符串[]
字符串
?为什么要在这里传递
int N
?为什么这需要递归?已经问过了吗?你要小心,这会导致
集合
不允许重复。这是一个家庭作业问题,必须使用递归来完成。@Kartik答案更新为使用列表,而不是集合。我们不限于在这个网站上给出固定的家庭作业答案,事实上,我给出的解决方案可能对在生产中工作的真正工程师非常有用。
集合
不允许重复。这是一个家庭作业问题,必须使用递归完成。@Kartik答案更新为使用列表,而不是集合。我们不限于givi在这个网站上,我给出了一个固定的家庭作业答案,事实上,我给出的解决方案可能对一个真正的生产工程师很有用。这看起来很好。那么我怎么称呼它呢?当我称呼它时,它不返回任何东西。这看起来很好。那么我怎么称呼它呢?当我称呼它时,它不返回任何东西。