0

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

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

}

}
4

1 回答 1

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()而不是让你的主对象做这项工作。代表!

于 2012-11-01T18:44:23.890 回答