Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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更改最终2D数组数据而不隐式更改其值_Java_Arrays_Try Catch - Fatal编程技术网

使用Java更改最终2D数组数据而不隐式更改其值

使用Java更改最终2D数组数据而不隐式更改其值,java,arrays,try-catch,Java,Arrays,Try Catch,我目前面临着一个关于getSurroundingSumGrid()方法的问题,该方法应该从基于文本文件数据构建的早期网格中获取数据,并使用它确定数组sumGrid中的新值。首先使用正确的值构建STATICGRID数组,但随着for循环的继续,STATICGRID值将更改为我设置的sumGrid值。我没有任何定义过的代码,其中STATICGRID被设置为等于另一个值,如果我这样做了,应该会给出一个错误 public double[][] getSurroundingSumGrid() {

我目前面临着一个关于getSurroundingSumGrid()方法的问题,该方法应该从基于文本文件数据构建的早期网格中获取数据,并使用它确定数组sumGrid中的新值。首先使用正确的值构建STATICGRID数组,但随着for循环的继续,STATICGRID值将更改为我设置的sumGrid值。我没有任何定义过的代码,其中STATICGRID被设置为等于另一个值,如果我这样做了,应该会给出一个错误

public double[][] getSurroundingSumGrid() {

    this.sumGrid = getBaseGrid();

    for (int rowNum = 0; rowNum < sumGrid.length; rowNum++) {
        final double[][] STATICGRID = this.getBaseGrid();
        double topNum = 0, botNum = 0, rightNum = 0, leftNum = 0;

        for (int colNum = 0; colNum < sumGrid[0].length; colNum++) {

            try {
                topNum = STATICGRID[rowNum - 1][colNum];
                System.out.println("TOPNUM : (" + (rowNum-1) + "," + colNum + ") " + STATICGRID[rowNum-1][colNum]);
            } catch (Exception e) {
                topNum = STATICGRID[rowNum][colNum];
                System.out.println("Top IndexOutOfBoundsException: " + STATICGRID[rowNum][colNum] + " used instead.");
            }

            try {
                botNum = STATICGRID[rowNum + 1][colNum];
                System.out.println("BOTNUM : (" + (rowNum+1) + "," + colNum + ") " + STATICGRID[rowNum+1][colNum]);
            } catch (Exception e) {
                botNum = STATICGRID[rowNum][colNum];
                System.out.println("Bot IndexOutOfBoundsException: " + STATICGRID[rowNum][colNum] + " used instead.");
            }

            try {
                leftNum = STATICGRID[rowNum][colNum - 1];
                System.out.println("LEFTNUM : (" + rowNum + "," + (colNum-1) + ") " + STATICGRID[rowNum][colNum-1]);
            } catch (Exception e) {
                leftNum = STATICGRID[rowNum][colNum];
                System.out.println("Left IndexOutOfBoundsException: " + STATICGRID[rowNum][colNum] + " used instead.");
            }

            try {
                rightNum = STATICGRID[rowNum][colNum + 1];
                System.out.println("RIGHTNUM : (" + rowNum + "," + (colNum+1) + ") " + STATICGRID[rowNum][colNum+1]);
            } catch (Exception e) {
                rightNum = STATICGRID[rowNum][colNum];
                System.out.println("Right IndexOutOfBoundsException: " + STATICGRID[rowNum][colNum] + " used instead.");
            }

            this.sumGrid[rowNum][colNum] = topNum + botNum + rightNum + leftNum;
            System.out.println("STATICGRID NEW NUM : " + STATICGRID[rowNum][colNum]);
            System.out.println("SUMGRID NEW NUM : " + sumGrid[rowNum][colNum]);
        }

    }

    return this.sumGrid;
}
public double[]getSurroundingSumGrid(){
this.sumGrid=getBaseGrid();
for(int-rowNum=0;rowNum
在使用代码进行这些测试时,我可以非常清楚地看到,两个数组中的数据都在超时更改,从而导致错误的结果。我试了大约两个小时,只是把东西搬来搬去,似乎不知道如何让它正常工作

您甚至可以看到,我甚至在每次for循环完成时都尝试重建STATICGRID数组,这甚至不会影响结果。无论您将STATICGRID放在何处,它都执行相同的操作(在for循环最顶层的外部或内部,无论它是最终的还是非最终的),它执行相同的操作。在看了这么长时间之后,我对我的代码为什么不工作感到非常困惑,我有一种轻微的感觉,那就是try-catch语句,但我根本不知道为什么。我对这个语句以及它的全部功能一无所知,但它存在的原因是因为数据可以得到一个IndexOutOfBoundsException,所以它会根据赋值说明计算它得到的每个IndexOutOfBoundsException


谢谢,我希望这是有意义的。

好吧,多亏弗雷德克建议使用deepCopy,我做了一些研究,并使用这种方法获得了更好的结果。这是未加密的深拷贝。我并不完全理解它,但我现在将使用它,并花一些时间学习更多关于它的知识以及它们是如何工作的。我目前是CS221的学生,我们还没有复习过deepCopy

public static Object deepCopy(Object orig) {
    Object obj = null;
    try {
        // Write the object out to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(orig);
        out.flush();
        out.close();

        // Make an input stream from the byte array and read
        // a copy of the object back in.
        ObjectInputStream in = new ObjectInputStream(
            new ByteArrayInputStream(bos.toByteArray()));
        obj = in.readObject();
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    catch(ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
    }
    return obj;
}
在这个方法之后,我就能够通过在getBaseGrid()方法前面使用它来阻止数组改变值

public double[]getSurroundingSumGrid(){
this.sumGrid=(double[])GridMonitor.deepCopy(this.getBaseGrid());
double[][]staticGrid=(double[][])GridMonitor.deepCopy(this.getBaseGrid());
double-topNum、botNum、rightNum、leftNum;
for(int-rowNum=0;rowNum
谢谢

getBaseGrid()方法是否生成2D数组的深度副本,而不仅仅返回对该数组的引用?如果不是,sumGrid和STATICGRID是对同一事物的引用。
public double[][] getSurroundingSumGrid() {

    this.sumGrid = (double[][]) GridMonitor.deepCopy(this.getBaseGrid());
    double[][] staticGrid = (double[][]) GridMonitor.deepCopy(this.getBaseGrid());
    double topNum, botNum, rightNum, leftNum;

    for (int rowNum = 0; rowNum < sumGrid.length; rowNum++) {

        for (int colNum = 0; colNum < sumGrid[0].length; colNum++) {

            try {
                topNum = staticGrid[rowNum - 1][colNum];
            } catch (Exception e) {
                topNum = staticGrid[rowNum][colNum];
            }

            try {
                botNum = staticGrid[rowNum + 1][colNum];
            } catch (Exception e) {
                botNum = staticGrid[rowNum][colNum];
            }

            try {
                leftNum = staticGrid[rowNum][colNum - 1];
            } catch (Exception e) {
                leftNum = staticGrid[rowNum][colNum];
            }

            try {
                rightNum = staticGrid[rowNum][colNum + 1];
            } catch (Exception e) {
                rightNum = staticGrid[rowNum][colNum];
            }

            this.sumGrid[rowNum][colNum] = topNum + botNum + rightNum + leftNum;
        }

    }
    return this.sumGrid;
}