我究竟做错了什么? (java.lang.ArrayIndexOutOfBoundsException)

时间:2019-05-06 04:48:03

标签: java multidimensional-array nested-loops array-indexing

我的代码应该找到具有相同值的四个连续数字,满足以下四种情况:(源代码附在底部
图a(对应于方法 isConsecutiveFourinStacks
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 6 2 1 8 2 9
6 5 6 1 1 9 1
1 3 6 1 4 0 7
3 3 3 3 4 0 7

fig.b(对应于方法 isConsecutiveFourinColumns
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 5 2 1 8 2 9
6 5 6 1 1 9 1
1 5 6 1 4 0 7
3 5 3 3 4 0 7

fig.c(对应于方法 isConsecutiveFourinReverseDiagonal
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 6 2 1 6 2 9
6 5 6 6 1 9 1
1 3 6 1 4 0 7
3 6 3 3 4 0 7

fig.d(对应于方法 isConsecutiveFourinDiagonal
0 1 0 3 1 6 1
0 1 6 8 6 0 1
9 6 2 1 8 2 9
6 9 6 1 1 9 1
1 3 9 1 4 0 7
3 3 3 9 4 0 7

到目前为止,除了 fig.c
之外,我已经能够按预期进行 我知道我可以翻转数组以使其表现得像 fig.d 一样,并对 fig.d 使用现有方法来获得所需的结果。
在这种特定情况下,我试图从数组的底部到顶部以及从左到右读取数据,这将导致类似于 fig.c 的图形。
但是我继续走出界限错误( 128行)。

解决此问题的目的是:
1.了解多维数组索引。 2.了解嵌套的for循环如何相互影响

我只是一个学习计算机科学学士学位的学生,具有C和C ++的先验知识,但是从未对编码的这两个方面有清楚的了解。任何提示将不胜感激。

package patternRecognition;

import java.util.Scanner;

public class ConsecutiveFoursInMatrix 
{
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) 
    {
        int rows,columns;

        System.out.println("Enter number of elements in a rows: ");
        rows = input.nextInt();
        System.out.println("Enter number of columns: ");
        columns = input.nextInt();

        int[][] matrix = new int[columns][rows];

        System.out.println("Start entering "+rows+" number of elements in "+columns+" stack(s): ");

        for(int i=0; i<=columns-1; i++)
        {
            for(int j=0; j<=rows-1; j++)
            {
                matrix[i][j]=input.nextInt();
            }
        }
        System.out.println("The matrix you entered: ");

        for(int i=0; i<=columns-1; i++)
        {
            for(int j=0; j<=rows-1; j++)
            {
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }

        //if(isConsecutiveFourinStacks(matrix,rows,columns)||isConsecutiveFourinColumns(matrix,rows,columns)||isConsecutiveFourinDiagonal(matrix,rows,columns))
        if(isConsecutiveFourinReverseDiagonal(matrix,rows,columns))
        {
            System.out.println("Series has consecutive fours");
        }
        else System.out.println("Series does not have consecutive fours");  
    }
    public static boolean isConsecutiveFourinStacks(int[][] matrix, int rows, int columns)
    {
        int count = 0;
        int consecutives = 0;
        for(int i=0; i<=columns-1; i++)
        {
            for(int j=0; j<=rows-2; j++)//rows-2 to avoid array out of bound error
            {
                if(matrix[i][j]==matrix[i][j+1])
                {
                    count++;
                    if (count==3)
                    {
                        consecutives++;
                        count = 0;
                    }
                }
                else count = 0;
            }
        }
        if(consecutives>0) return true;
        else return false;
    }
    public static boolean isConsecutiveFourinColumns(int[][] matrix, int rows, int columns)
    {
        int count = 0;
        int consecutives = 0;
        for(int j=0; j<=rows-1; j++)
        {
            for(int i=0; i<=columns-2; i++)
            {
                if(matrix[i][j]==matrix[i+1][j])
                {
                    count++;
                    if(count==3)
                    {
                        consecutives++;
                        count = 0;
                    }
                }
                else count = 0;
            }
        }
        if (consecutives>0) return true;
        else return false;
    }
    public static boolean isConsecutiveFourinDiagonal(int[][] matrix, int rows, int columns)
    {
        int count = 0;
        int consecutives = 0;
        for(int column=0; columns-column>=4; column++)
        {
            for(int row=0; rows-row>=4; row++)
            {
                for(int i=column,j=row; columns-i>=2 && rows-j>=2; i++,j++)
                {
                    if(matrix[i][j]==matrix[i+1][j+1])
                    {
                        count++;
                        if(count==3)
                        {
                            consecutives++;
                            count=0;
                        }
                    }
                    else count=0;
                }
            }   
        }
        if (consecutives>0) return true;
        else return false;
    }
    public static boolean isConsecutiveFourinReverseDiagonal(int[][] matrix, int rows, int columns)
    {
        int count = 0;
        int consecutives = 0;
        for(int column=columns-1; column>=0; column--)
        {
            for(int row=0; row<=rows-1; row++)
            {       
                for(int i=column,j=row; columns-i>=2 && j<=rows-2; i--,j++)
                {
                    if(matrix[i][j]==matrix[i-1][j+1])
                    {
                        count++;
                        if(count==3)
                        {
                            consecutives++;
                            count=0;
                        }
                    }
                    else count=0;
                }
            }
        }
        if (consecutives>0) return true;
        else return false;
    }
}


  [1]: https://i.stack.imgur.com/dgxjk.png

1 个答案:

答案 0 :(得分:0)

在isConsecutiveFourinReverseDiagonal方法的最内层循环中,只需检查i值是否大于0, 将i>0放在最内层循环的检查步骤中:

for(int i=column,j=row; columns-i>=2 && j<=rows-2 && i>0; i--,j++){
     if(matrix[i][j]==matrix[i-1][j+1]){
        count++;
        if(count==3){
          consecutives++;
          count=0;
        }
     } 
     else count=0;
}