java 尝试为每个值将 1 添加到整数数组。爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15933596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:20:05  来源:igfitidea点击:

Trying to add 1 to an integer array for every value. JAVA

javaarraysint

提问by Greg Flint

(int i=0; i < n; i++){
for(int j=0; j < n; j++){
    Array[i][j]=Array[i][j] + 1;
    }
}

for some reason I am getting an error. Array is an int array filled with 0s. n can be any size. The error i keep getting is exception in main thread for the Array line. Am I not adding 1 to the array correctly?

出于某种原因,我收到一个错误。Array 是一个用 0 填充的 int 数组。n 可以是任意大小。我不断收到的错误是 Array 行的主线程中的异常。我没有正确地将 1 添加到数组中吗?

edit: error was called exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 3 at (where that one line is)

编辑:错误在线程“main”中被称为异常 java.lang.ArrayIndexOutofBoundsException: 3 at (那一行所在的位置)

edit: it was initialized with

编辑:它被初始化为

       Array = new int[n][n];
    for(int i=0; i < n; i++){
        for(int j=0; j < n; j++){
            Array[i][j] = 0;
        }
    }

but this doesn't seem to be a part of the error

但这似乎不是错误的一部分

回答by Jon Skeet

edit: error was called exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 3

编辑:错误在线程“main”中被称为异常 java.lang.ArrayIndexOutofBoundsException: 3

Right. That means that your array isn't big enough. It's not clear whether the firstdimension is only 2 or the second, but one of them isn't correct. You should have something like:

对。这意味着您的阵列不够大。不清楚第一维是只有 2 维还是第二维,但其中一个是不正确的。你应该有类似的东西:

int[][] array = new int[n][n];

somewhere. If you haven't, you should look at what you've actuallygot.

某处。如果你还没有,你应该看看你实际得到了什么。

A better alternative - would be to use the length of the array in the forloop as the bound:

更好的选择 - 将使用for循环中数组的长度作为边界:

for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length; j++) {
        array[i][j] = array[i][j] + 1;
    }
}

Or even:

甚至:

for (int i = 0; i < array.length; i++) {
    int[] row = array[i];
    for (int j = 0; j < row.length; j++) {
        row[j] = row[j] + 1;
    }
}

Or:

或者:

for (int i = 0; i < array.length; i++) {
    int[] row = array[i];
    for (int j = 0; j < row.length; j++) {
        row[j]++;
    }
}

EDIT: You now claimit really was initialized like this:

编辑:您现在声称它确实是这样初始化的:

Array = new int[n][n];

If that's the case, then the value of nmust have been changed since initialization, so it's effectively irrelevant. If the value of nhasn't changed, your original code simply won't throw that exception.

如果是这种情况,那么n自初始化以来的值肯定已更改,因此它实际上无关紧要。如果 的值n没有改变,你的原始代码就不会抛出那个异常。

回答by Subhrajyoti Majumder

Use proper length, Array.lengthwill be length of 1d of the Arrayand Array[i].lengthwill be length of 2d -

使用适当的长度,Array.length将是的1D长度ArrayArray[i].length将2d的长度-

for(int i=0; i < Array.length; i++){
    for(int j=0; j < Array[i].length; j++){
        Array[i][j]=Array[i][j] + 1;
    }
}

回答by Steffen Heil

If it really was initialized like this:

如果它真的是这样初始化的:

int[][] Array;
Array = new int[n][n];
for(int i=0; i < n; i++){
    for(int j=0; j < n; j++){
        Array[i][j] = 0;
    }
}

Andif neither Arraynor nnor Array[i]for any iwere changed inbetween then the following code willwork:

而且,如果没有Array,也没有n,也没有Array[i]任何i被改变其间的,则下面的代码工作:

for(int i=0; i < n; i++){
    for(int j=0; j < n; j++){
        Array[i][j]=Array[i][j] + 1;
    }
}

So either your code is changing one of the things mentioned or your question is bogus...

因此,要么您的代码正在更改提到的其中一项内容,要么您的问题是虚假的...

(BTW: I strongly recommend calling that Arraydifferent, at least call it array...)

(顺便说一句:我强烈建议称其为Array不同的,至少称其为array......)