简体   繁体   English

Java 2D数组矩形错误

[英]Java 2D array rectangle error

I have an addition to a question I asked earlier. 我之前提到的问题还有一个补充。 I have a 2D array that I need to get the magnitude of each element based on the 4 surrounding elements. 我有一个2D数组,我需要根据周围的4个元素得到每个元素的大小。 Surrounding being up, down, left and right. 周围是上,下,左,右。 If one or more of the surrounding elements goes out of the array bounds, ie the current element is on an edge, it treats the out of bounds element as the current. 如果周围元素中的一个或多个超出数组边界,即当前元素位于边缘上,则将超出边界元素视为当前元素。 My program only works when the array is a square, 4 x 4, 5 x 5, ect. 我的程序仅在阵列为正方形,4 x 4,5 x 5等时才有效。 but when it is a rectagle, 4 x 5, 5 x 6, ect I get an error. 但是当它是直肠时,4 x 5,5 x 6等,我得到一个错误。 I believe this is due to the fact that array.length is no longer the same for x and y. 我相信这是因为x和y的array.length不再相同。 I do not know how to correct this error, any help would be appreciated! 我不知道如何纠正这个错误,任何帮助将不胜感激! This is my current code: 这是我目前的代码:

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

}

}

First of all, your problem is that you are using array.length for both of your indices. 首先,您的问题是您正在为两个索引使用array.length You should use array[0].length for your y coordinate. 您应该使用array[0].length作为y坐标。 You do this in a number of places, that's why it doesn't work for rectangles. 你在很多地方这样做,这就是为什么它不适用于矩形。

However, you would be far better served to take an OO approach to this kind of problem. 但是,对于这种问题采取OO方法会更好。 Try this: 尝试这个:

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

Do you see how letting the objects do the work for you eliminates a lot of copy-paste code and if statements? 你是否看到让对象为你做的工作如何消除了大量的复制粘贴代码和if语句? Teach an object how to do a job, then say MyObject.doJob() instead of having your master object do the job. 教一个对象如何做一个工作,然后说MyObject.doJob()而不是让你的主对象完成这项工作。 Delegate! 代表!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM