Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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_Arrays_Recursion_Overloading_Helper - Fatal编程技术网

Java 增加索引后如何递归添加?

Java 增加索引后如何递归添加?,java,arrays,recursion,overloading,helper,Java,Arrays,Recursion,Overloading,Helper,我目前正在做一个家庭作业,目标是只做递归(没有循环)。我确信我可以重载并添加助手方法,这是我完成这项工作的唯一方法 所以问题是,我有一个int数组A={1,2,3,4}(或者类似的东西),我需要用{10,9,7,4}生成一个返回数组 10=1+2+3+4 9=2+3+4 7=3+4 4=4 我想过使用这样的东西(不是工作代码) int计数器=0; 公共整数[]r(整数[]个数){ 返回r(数字、计数器); } 公共int[]r(int[]number,int索引){ 整数和=0; //检查下一个

我目前正在做一个家庭作业,目标是只做递归(没有循环)。我确信我可以重载并添加助手方法,这是我完成这项工作的唯一方法

所以问题是,我有一个int数组A={1,2,3,4}(或者类似的东西),我需要用{10,9,7,4}生成一个返回数组

10=1+2+3+4

9=2+3+4

7=3+4

4=4

我想过使用这样的东西(不是工作代码)

int计数器=0;
公共整数[]r(整数[]个数){
返回r(数字、计数器);
}
公共int[]r(int[]number,int索引){
整数和=0;
//检查下一个值是否存在,否则结束它
//这将是一个助手方法,而不是for循环

对于(intx=index;x您的函数是不正确的

public int[] r(int[] numbers, int index){

    //--> needs a stop condition e.g. index==numbers.length, index==0

    int sum = 0;
    // base case to check if next value exists otherwise end it

    // this would be a helper method instead of a for loop
    for(int x=index; x<numbers.length; x++){
        //--> numbers has been overwritten by the sum 
        // you should just do numbers[index] = numbers[index-1]+numbers[index]
        // this is opposite to the summation you wish for which starts adding 
        // from the last position
        sum += numbers[x];
    }


    numbers[index] = sum;
    index++;
    return r(numbers, index);
}
但由于覆盖了初始数组,因此会丢失它

您可以构建这样的函数

public int sumRecursive(int[] input, int[] output,int pos){
    if (pos==0){
        output[pos] = input[pos]
        return input[pos]
    }else{
        output[pos] = input[pos] + sumRecursive(input,output,pos-1);
        return output[pos];
     }
}
int[] output = new int[input.length];
sumRecursive(input,output,input.length);
你需要这样称呼它

public int sumRecursive(int[] input, int[] output,int pos){
    if (pos==0){
        output[pos] = input[pos]
        return input[pos]
    }else{
        output[pos] = input[pos] + sumRecursive(input,output,pos-1);
        return output[pos];
     }
}
int[] output = new int[input.length];
sumRecursive(input,output,input.length);

你的函数是不正确的

public int[] r(int[] numbers, int index){

    //--> needs a stop condition e.g. index==numbers.length, index==0

    int sum = 0;
    // base case to check if next value exists otherwise end it

    // this would be a helper method instead of a for loop
    for(int x=index; x<numbers.length; x++){
        //--> numbers has been overwritten by the sum 
        // you should just do numbers[index] = numbers[index-1]+numbers[index]
        // this is opposite to the summation you wish for which starts adding 
        // from the last position
        sum += numbers[x];
    }


    numbers[index] = sum;
    index++;
    return r(numbers, index);
}
但由于覆盖了初始数组,因此会丢失它

您可以构建这样的函数

public int sumRecursive(int[] input, int[] output,int pos){
    if (pos==0){
        output[pos] = input[pos]
        return input[pos]
    }else{
        output[pos] = input[pos] + sumRecursive(input,output,pos-1);
        return output[pos];
     }
}
int[] output = new int[input.length];
sumRecursive(input,output,input.length);
你需要这样称呼它

public int sumRecursive(int[] input, int[] output,int pos){
    if (pos==0){
        output[pos] = input[pos]
        return input[pos]
    }else{
        output[pos] = input[pos] + sumRecursive(input,output,pos-1);
        return output[pos];
     }
}
int[] output = new int[input.length];
sumRecursive(input,output,input.length);

您需要一个停止条件,以及之后检索数组的方法

public int sum(int old, int[] numbers, int index) {
    if (index == numbers.length) return old;
    return sum(numbers[index] + old, numbers, index + 1);
}

public int[] r(int[] numbers, int[] output, int count) {
    if (count == numbers.length) {
        return output;
    } else {
        output[count] = sum(0, numbers, 0);
        return r(numbers, output, count + 1);
    }
}

public int[] r(int[] numbers) {
    return r(numbers, new int[numbers.length], 0);
}

编辑:更改代码以消除对
循环的
需求

您需要一个停止条件,以及一种随后检索数组的方法

public int sum(int old, int[] numbers, int index) {
    if (index == numbers.length) return old;
    return sum(numbers[index] + old, numbers, index + 1);
}

public int[] r(int[] numbers, int[] output, int count) {
    if (count == numbers.length) {
        return output;
    } else {
        output[count] = sum(0, numbers, 0);
        return r(numbers, output, count + 1);
    }
}

public int[] r(int[] numbers) {
    return r(numbers, new int[numbers.length], 0);
}

编辑:更改代码以消除对
循环的
需求

我认为这将为您提供正确答案:

public static int[] slideAndSumArrayElements(int[] array, int[] result, int index) {
    // base case - stop when the index is same as the array.length
    if (index == array.length) {
        return result;
    } 
    else {
        // Add all elements of the array starting from the index position till length of the array and store the result in result[index]
        result[index] = addArrayElementsRecursively(array, index);

        // slide the main array by incrementing the index 
        return slideAndSumArrayElements(array, result, index + 1);
    }
}

public static int addArrayElementsRecursively(int[] arr, int index){
    // base case - when the index is same as the original array len stop
    if (arr.length == index){
        return 0;
    }

    // add progressively each element of the given array 
    return arr[index] + addArrayElementsRecursively(arr, index + 1);
}

public static void arraySum(){
    int[] array = {1, 2, 3, 4};
    int[] result = new int[array.length];

    result = slideAndSumArrayElements(array, result, 0);
}
输出数组或结果将为:
[10,9,7,4]

我想这会给你一个正确的答案:

public static int[] slideAndSumArrayElements(int[] array, int[] result, int index) {
    // base case - stop when the index is same as the array.length
    if (index == array.length) {
        return result;
    } 
    else {
        // Add all elements of the array starting from the index position till length of the array and store the result in result[index]
        result[index] = addArrayElementsRecursively(array, index);

        // slide the main array by incrementing the index 
        return slideAndSumArrayElements(array, result, index + 1);
    }
}

public static int addArrayElementsRecursively(int[] arr, int index){
    // base case - when the index is same as the original array len stop
    if (arr.length == index){
        return 0;
    }

    // add progressively each element of the given array 
    return arr[index] + addArrayElementsRecursively(arr, index + 1);
}

public static void arraySum(){
    int[] array = {1, 2, 3, 4};
    int[] result = new int[array.length];

    result = slideAndSumArrayElements(array, result, 0);
}
输出数组或结果将为:

[10, 9, 7, 4]

如果有人能帮我解答问题、标题等,我不知道该怎么称呼,因为这是我第一次遇到这个问题。我没有看到停止条件,您需要一个停止条件……您的问题很好。您可以更明确地指出,您正在查找递归的索引越界问题。但是其他ise制定得很好。如果有人能帮我回答问题、标题等,我不知道该怎么称呼它,因为这是我第一次遇到这个问题。我没有看到停止条件,你需要一个停止条件……你的问题很好。你可以更明确地指出,你正在寻找一个索引越界的问题除此之外,我应该说得更清楚,但我确实需要保留第一个方法r(int[]数字)。如果我通过该方法调用它,它还会工作吗?我应该说得更清楚,但我确实需要保留第一个方法r(int[]数字)。如果我通过该方法调用它,它还会工作吗?你会如何在该方法中不使用for循环。这也是我遇到的问题(在helper方法或其他方面)@Joe你其实不需要for循环。这里有一个聪明的窍门。@Jornverne我必须创建一个helper方法,因为我就是这么想的吗?@Joe不,它甚至比那更简单。你怎么能不在方法中使用for循环呢。这也是我遇到的问题(就helper方法或其他方面而言).@Joe你实际上不需要for循环。这里有一个聪明的技巧。@Jornverne我必须创建一个helper方法,因为这就是我所想的吗?@Joe不,它甚至比那更简单。嘿,它很有效,但我不仅仅是在寻找答案……解决问题时你的一些思考过程是什么。检查内联评论并如果你有问题,请告诉我。嘿,这很有效,但我不仅仅是在寻找答案……解决问题时你的一些思考过程是什么。检查内联评论,如果你有问题,请告诉我。