gpt4 book ai didi

Java 2D数组矩形错误

转载 作者:行者123 更新时间:2023-12-02 07:28:51 25 4
gpt4 key购买 nike

我对之前提出的问题进行了补充。我有一个 2D 数组,我需要根据 4 个周围元素获取每个元素的大小。周围有上、下、左、右。如果一个或多个周围元素超出数组边界,即当前元素位于边缘,则它将超出边界的元素视为当前元素。我的程序仅在数组为正方形(4 x 4、5 x 5 等)时才有效。但当它是矩形、4 x 5、5 x 6 等时,我会收到错误。我相信这是因为 x 和 y 的 array.length 不再相同。我不知道如何纠正这个错误,任何帮助将不胜感激!这是我当前的代码:

public class ArrayTest 
{


public static int[][] buildE(int[][] array)
{

int [][] arrayE = new int[array.length][array.length];

int up;
int down;
int left;
int right;

for (int y = 0; y < array.length; y++)
{
for (int x = 0; x < array[y].length; x++)
{

//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 - 1 && x == array.length - 1)
{

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 - 1)
{

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 - 1 && 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 - 1)
{

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 - 1)
{

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];

}

int element = array[y][x];
int magnitude = Math.abs(element - up) + Math.abs(element - down) + Math.abs(element - left) + Math.abs(element - right);

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);
System.out.println("Magnitude: " + magnitude);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Array Length: " + array.length);


arrayE[y][x] = magnitude;
}
}

return arrayE;

}


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}, {6, 62, 63, 64, 65}};

outputArray(myArray);
outputArray(buildE(myArray));

}

}

最佳答案

首先,您的问题是您对两个索引都使用 array.length 。您应该使用 array[0].length 作为您的 y 坐标。您可以在很多地方执行此操作,这就是它不适用于矩形的原因。

但是,如果采用面向对象的方法来解决此类问题,效果会更好。试试这个:

public class ArrayTest {
public static enum Direction {
LEFT, RIGHT, UP, DOWN, SELF;

public int getValue(int[][] array, int yIndex, int xIndex) {
switch(this) {
case LEFT:
if (xIndex == 0) return array[yIndex][xIndex];
return array[yIndex][xIndex - 1];
case RIGHT:
if (xIndex == array[yIndex].length - 1) return array[yIndex][xIndex];
return array[yIndex][xIndex + 1];
case UP:
if (yIndex == 0) return array[yIndex][xIndex];
return array[yIndex - 1][xIndex];
case DOWN:
if (yIndex == array.length - 1) return array[yIndex][xIndex];
return array[yIndex + 1][xIndex];
default:
return array[yIndex][xIndex];
}
}
}

public static int[][] buildE(int[][] array) {
int [][] arrayE = new int[array.length][array[0].length];

for (int y = 0; y < array.length; y++) {
System.out.println("y = " + y);
for (int x = 0; x < array[y].length; x++) {
arrayE[y][x] = 0;
for (Direction d : Direction.values()) {
arrayE[y][x] += d.getValue(array, y, x);
}
}
}

return arrayE;
}

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}, {6, 62, 63, 64, 65}};

outputArray(myArray);
outputArray(buildE(myArray));
}
}

您是否明白让对象为您完成工作如何消除大量复制粘贴代码和 if 语句?教一个对象如何完成一项工作,然后说 MyObject.doJob() 而不是让您的主对象完成这项工作。代表!

关于Java 2D数组矩形错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177149/

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