我之前提到过一个问题。我有一个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));
}
}
答案 0 :(得分:2)
首先,您的问题是您对两个索引都使用array.length
。您应该使用array[0].length
作为y
坐标。你可以在很多地方这样做,这就是为什么它不适用于矩形。
但是,对于这种问题采取OO方法会更好。试试这个:
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()
而不是让你的主对象完成这项工作。委派!