Java的二维数组越界错误 [英] Java 2D array out of bounds error

查看:139
本文介绍了Java的二维数组越界错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序,用一个二维数组交易,确实根据周围的元素计算,然后创建一个新的数组。要开始我创建了一个简单的java程序来测试不同的情况。我想与如果元素是在阵列的边缘正确对待,如果是这样设置上,下,左,右等于元素。我的测试程序工作的元件是在顶,左,或不上的边缘,但不为底部或右。这是我目前的code:

I am writing a program that deals with a 2D array, does calculations based on surrounding elements and then creates a new array. To start I created a simple java program to test different cases. I want to correctly deal with if the element is on an edge of the array and if so set the "up, down, left, or right" equal to that element. My test program works for the element being on the top, left, or not on an edge, but not for the bottom or right. This is my current code:

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 。任何帮助将是非常美联社preciated!

Furthermore if I set y = 4 it does not recognize this as myArray.length. However I do not think this will be an issue in my actual program if I iterate through until array.length. Any help would be much appreciated!

推荐答案

数组索引从0开始,如果您的数组的长度为5,最后elemnt将在第4索引访问。在code

array indexes start from 0, if your array is of length 5, the last elemnt will be accessed at 4th index. in your code

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

    }

按照其他否则,如果是同样的做法。

follow the same approach in other else if's.

这篇关于Java的二维数组越界错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆