Java 2D数组错误

Java 2D数组错误,java,Java,因此,我需要使用一个二维数组对每个元素进行计算,并在使用当前元素的“left”、“right”、“up”和“down”的值时将其转换为另一个二维数组。如果当前元素位于边缘(x=0,y=0,x=array.length,y=array.length),我将得到一个数组越界错误。我想创建一个for循环来处理每种情况,但我不知道怎么做。下面是我的代码示例 private void buildE(int[][] array, int y, int x) { int up = array[y -

因此,我需要使用一个二维数组对每个元素进行计算,并在使用当前元素的“left”、“right”、“up”和“down”的值时将其转换为另一个二维数组。如果当前元素位于边缘(x=0,y=0,x=array.length,y=array.length),我将得到一个数组越界错误。我想创建一个for循环来处理每种情况,但我不知道怎么做。下面是我的代码示例

private void buildE(int[][] array, int y, int x)
{

    int up = array[y - 1][x];
    int down = array[y + 1][x];
    int left = array[y][x - 1];
    int right = array[y][x + 1];

    if(up == 0){

        buildETopRow(array);

    }

E将是我的新阵列。此方法不起作用,因为y不等于0,它只是不存在,但我也不能将int设置为null。在发生越界错误的情况下,我需要越界的元素(上、下、左或右)与当前元素相等。有没有一种方法我仍然可以使用for循环来实现此目的,或者我需要做其他事情?

用边界区域包围阵列的最简单方法。因此,您的
x
维度实际上是
width+2

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int realWidth = 10;
        int realHeight = 10;
        int[][] in = new int[(realWidth+2)][(realHeight+2)];
        int[][] out = new int[(realWidth+2)][(realHeight+2)];
        for (int j = 1;j<realHeight+1;j++)
        {
            for (int i = 1;i<realWidth+1;i++)
            {
                int top = in[j-1][i];
                int bottom = in[j+1][i];
                int left= in[j][i-1];
                int right  = in[j][i+1];
                out[j][i] = operation(top,bottom,left,right);
            }
        }
    }
    public static int operation (int top,int bottom,int left,int right)
    {
        return top+bottom+left+right;
    }
}
import java.util.*;
导入java.lang.*;
班长
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
int realWidth=10;
int realHeight=10;
int[]in=newint[(realWidth+2)][(realHeight+2)];
int[]out=newint[(realWidth+2)][(realHeight+2)];

对于(int j=1;j如果我读得正确,你会希望有效地将边上的元素与边外的元素的差视为0。如果是这样,我将编写四个方法right()、left()、up()和down(),下面以down()为例:

 /*
  * Return the difference between an element an the element below it
  */

public void down(int x, int y) {

    if (y == array.length - 1) { 
       \\ on the bottom edge
       return 0;
    }   

    return array[y][x] - array[y + 1][x];

}
在你的循环中,你会计算:

up(x,y) + down(x,y) + right(x,y) + left(x,y)

我不能完全确定你的问题是什么,但是(1)遍历2D数组的常用结构是使用嵌套for循环(一个在另一个内部),以及(2)当你需要环绕计数器(例如2,3,0,1,2,…)时,使用余数操作符
%

int numRows = theArray.length;
int numCols = theArray[0].length;

for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {

        int right = theArray[(j+1) % numCols];
        int down = theArray[(i+1) % numRows];
        int left = theArray[(j+numCols-1) % numCols];
        int up = theArray[(i+numRows-1) % numCols];

        /* right, down, left, and up will be the elements to the right, down, 
           left, and up of the current element. Npw that you have them, you can 
           process them however you like and put them in the other array. */

    }
}
int numRows=数组长度;
int numCols=theArray[0]。长度;
对于(int i=0;i

余数运算符
A%B
所做的是在A与B一样大时将其设置回零。由于B是数组的大小,因此正好是当它太大时,会导致IndexOutOfBounds错误。注意:
%
不是这样工作的,但考虑它的作用是可以的。要了解更多有关它的信息,可以用谷歌搜索它,我找到了一个合适的解释。

当访问边缘以外的元素时,您希望发生什么?它应该环绕到另一个边缘,还是有一个默认值,还是什么?我想做的是将E中的集合[I][j]设置为原始数组中这些元素的大小。因此E[I][j]=I][j中的差值,上、下、左、右加在一起。抱歉,如果这很难理解,我编辑了我的原始问题以澄清。我需要将超出范围的元素视为当前元素,但这回答了我的问题。谢谢!