递归方法以替换二维数组中所有出现的值

huangapple go评论80阅读模式
英文:

Recursive method to replace all occurrences of a value in a 2D array

问题

private static int replaceAll(double number, double replacementTerm) {
    int i = 0;
    int j = 0;
    double searchFor = number;
    double replace = replacementTerm;

    if (i == 1 && j == 2) {
        System.out.println("Search complete!");
    }

    if (twoDimArray2[i][j] == searchFor) {
        System.out.println("Replaced An Element!");
        twoDimArray2[i][j] = replace;
        System.out.println(twoDimArray2[i][j]);
        j++;
        return replaceAll(searchFor, replace);
    }

    if (j == twoDimArray2.length) {
        i++;
        return replaceAll(searchFor, replace);
    } else {
        j++;
        return replaceAll(searchFor, replace);
    }
}
英文:

I have created a recursive method that replaces all occurrences of an element in a two dimensional double array. The issue is that I cannot seem to get this working without encountering a stack overflow error. Could someone please look at my code below and show me how to fix this? I have tried setting this up several times over the past few days. Thank you. Note that my arrays are 2 x 3, so the first if means that if you are at column 1 row 2, you are at the end of the array, and in that case you are done searching.

private static int replaceAll(double number, double replacementTerm) {
    int i = 0;
    int j = 0;
    double searchFor = number;
    double replace = replacementTerm;

    if (i == 1 && j == 2) {
        System.out.println("Search complete!");
    }

    if (twoDimArray2[i][j] == searchFor) {
        System.out.println("Replaced An Element!");
        twoDimArray2[i][j] = replace;
        System.out.println(twoDimArray2[i][j]);
        j++;
        return replaceAll(searchFor, replace);
    }

    if (j == twoDimArray2.length) {
        i++;
        return replaceAll(searchFor, replace);
    } else {
        j++;
        return replaceAll(searchFor, replace);
    }
}

答案1

得分: 1

ij 应该作为方法参数而不是局部变量,以便可以跟踪它们的值的更改。如果不超出数组的边界,尝试向右和向下递归移动。请注意,这比使用两层 for 循环进行迭代效率要低得多,因为它会多次检查数组中的多个位置;为了减轻这个问题,可以使用一个访问数组来存储以前访问过的所有位置,这样它们就不会被再次检查。查看下面的代码示例 here

private static void replaceAll(double number, double replacementTerm, int i, int j) {
    double searchFor = number;
    double replace = replacementTerm;
    if (twoDimArray2[i][j] == searchFor) {
        System.out.println("Replaced An Element!");
        twoDimArray2[i][j] = replace;
        System.out.println(twoDimArray2[i][j]);
    }
    if (i == twoDimArray2.length - 1 && j == twoDimArray2[0].length - 1) {
        System.out.println("Reached the end!");
        return;
    }
    if (i + 1 < twoDimArray2.length) {
        replaceAll(number, replacementTerm, i + 1, j);
    }
    if (j + 1 < twoDimArray2[0].length) {
        replaceAll(number, replacementTerm, i, j + 1);
    }
}
英文:

i and j should be method parameters instead of local variables so changes to their values can be tracked. Try to move right and down recursively if it does not exceed the bounds of the array. Note that this is much less efficient that iteration with two layers of for loops, as it will check multiple positions in the array more than once; to mitigate this, one can use a visited array to store all positions previous visited so they will not be checked again. See the below code in action here.

private static void replaceAll(double number, double replacementTerm, int i, int j) {
    double searchFor = number;
    double replace = replacementTerm;
    if (twoDimArray2[i][j] == searchFor) {
        System.out.println(&quot;Replaced An Element!&quot;);
        twoDimArray2[i][j] = replace;
        System.out.println(twoDimArray2[i][j]);
    }
    if (i == twoDimArray2.length - 1 &amp;&amp; j == twoDimArray2[0].length - 1) {
        System.out.println(&quot;Reached the end!&quot;);
        return;
    }
    if (i + 1 &lt; twoDimArray2.length) {
        replaceAll(number, replacementTerm, i + 1, j);
    }
    if (j + 1 &lt; twoDimArray2[0].length) {
        replaceAll(number, replacementTerm, i, j + 1);
    }
}

huangapple
  • 本文由 发表于 2020年6月29日 08:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62629711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定