gpt4 book ai didi

Java 二维数组越界错误

转载 作者:行者123 更新时间:2023-11-30 06:27:46 25 4
gpt4 key购买 nike

我正在编写一个处理二维数组的程序,根据周围的元素进行计算,然后创建一个新数组。首先,我创建了一个简单的 Java 程序来测试不同的情况。我想正确处理元素是否位于数组的边缘,如果是,则将 "up, down, left, or right" 设置为等于该元素。我的测试程序适用于位于顶部、左侧或不在边缘的元素,但不适用于底部或右侧。这是我当前的代码:

public class ArrayTest 
{
public static void buildE(int[][] array, int y, int x)
{
int up;
int down;
int left;
int right;

//if element is on the top left
if (y == 0 && x == 0)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x];
right = array[y][x + 1];
}

//if element is on bottom right
else if (y == array.length && x == array.length)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x - 1];
right = array[y][x];
}

//if element is on top right
else if(y == 0 && x == array.length)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x];
}

//if element is on bottom left
else if (y == array.length && x == 0)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x];
right = array[y][x + 1];
}

//if element is on top
else if (y == 0)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x + 1];
}

//if element is on left
else if (x == 0)
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x];
right = array[y][x + 1];
}

//if element is on bottom
else if(y == array.length)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x - 1];
right = array[y][x + 1];
}

//if element is on right
else if (x == array.length)
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x];
}

//if element is not on an edge
else
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x + 1];
}

System.out.println();
System.out.print("#####################################");
System.out.println();
System.out.println("Array Element: " + array[y][x]);
System.out.println("Up: " + up);
System.out.println("Down: " + down);
System.out.println("Left: " + left);
System.out.println("Right: " + right);
}

public static void outputArray(int[][] array)
{
for(int row = 0; row < array.length; row ++)
{
for (int column = 0; column < array[row].length; column++)
System.out.printf("%d ", array[row][column]);
System.out.println();
}
}

public static void main(String[] args)
{
int [][] myArray = {{1, 12, 13, 14, 15}, {2, 22, 23, 24, 25},
{3, 32, 33, 34, 35}, {4, 42, 43, 44, 45}, {5, 52, 53, 54, 55}};
outputArray(myArray);
buildE(myArray, 4, 0);
}
}

此外,如果我设置 y = 4,它不会将其识别为 myArray.length。但是,如果我遍历直到 array.length,我认为这在我的实际程序中不会成为问题。任何帮助将非常感激!

最佳答案

数组索引从 0 开始,如果您的数组长度为 5,则最后一个元素将在第 4 个索引处访问。在你的代码中

  else if (y == array.length && x == array.length)
{

up = array[y - 1][x];//Exception here
down = array[y][x];
left = array[y][x - 1];
right = array[y][x];

}

将其更改为:

 else if (y == array.length-1 && x == array.length-1)
{

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

}

在其他 else if 中遵循相同的方法。

关于Java 二维数组越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13130538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com