【问题标题】:Exception in thread main Java线程主 Java 中的异常
【发布时间】:2022-01-19 13:03:09
【问题描述】:

这是我的程序以顺时针螺旋方式打印从 1 到 N^2 的自然数。我收到以下错误

     Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 
out of bounds for length 3
    at Spiral.main(Spiral.java:13)

这是我的程序

class Spiral{
   public static void main(String[] args) {
       System.out.println("Enter value of N"); 
       Scanner sc=new Scanner(System.in);
       int N=sc.nextInt();
       int arr[][]=new int[N][N];
       int r1=0, c1=0, r2=N-1, c2=N-1, flag=1; int i=0,j=0;
       while(flag<=N*N)
      { for(j=c1;j<=c2;j++)
       arr[r1][j]=flag++;
       
       
       for( i=r1+1;i<=r2;i++)
        arr[i][c2]=flag++;  //this is the line of error
       
       
       for(j=c2-1;j>=c1;j--)
        arr[r2][j]=flag++;
       
       
       for(i=r2-1; i>r1+1;i--)
        arr[i][c1]=flag++;
       
       
       r1++; r2--; c1++; c2--; 
      }
       System.out.println("The Circular Matrix is:");
for( i=0;i<N;i++)
{
for( j=0;j<N;j++)
{
System.out.print(arr[i][j]+ "\t");
}
System.out.println();
} 
}
}

代码在 N=2 时工作正常,但在 N=3,4 等情况下开始出现此错误。如果 N=3,arr[i][c2] 的最大值将是 arr[2][2]落在 3x3 矩阵的范围内。有人可以解释为什么我会收到这个错误吗?

【问题讨论】:

    标签: java arrays error-handling indexoutofboundsexception spiral


    【解决方案1】:

    如果您的start row 大于end rowstart col 大于end col,则根据您的逻辑,您需要退出。

    将此添加到您的 while 循环中

    if (r1 > r2 || c1 > c2) break;
    

    【讨论】:

      【解决方案2】:

      在挖掘器中,您可以看到您在第二个 fpr 循环中有一个 IndexOutOfBoundsException,因为 i 是 3。由于 while 循环始终为真,因为 for 循环的条件都不为真,r1 和 c1 将执行直到 int 最大值然后溢出。你的for循环错了

      【讨论】:

        【解决方案3】:

        在相应的 for 循环之后而不是在 while 循环的末尾适当地增加/减少 r1c2r2c1

        import java.util.Scanner;
        
        class Spiral {
        
            public static void main(String[] args) {
                System.out.println("Enter value of N");
                Scanner sc = new Scanner(System.in);
                int N = sc.nextInt();
                int[][] arr = new int[N][N];
                int r1 = 0, c1 = 0, r2 = N - 1, c2 = N - 1, flag = 1;
                int i = 0, j = 0;
                while (flag <= N * N) {
                    for (j = c1; j <= c2; j++)
                        arr[r1][j] = flag++;
                    r1++;
                    for (i = r1; i <= r2; i++)
                        arr[i][c2] = flag++;
                    c2--;
                    for (j = c2; j >= c1; j--)
                        arr[r2][j] = flag++;
                    r2--;
                    for (i = r2; i >= r1; i--)
                        arr[i][c1] = flag++;
                    c1++;
                }
                System.out.println("The Circular Matrix is:");
                for (i = 0; i < N; i++) {
                    for (j = 0; j < N; j++) {
                        System.out.print(arr[i][j] + "\t");
                    }
                    System.out.println();
                }
            }
        
        }
        

        示例用法 1:

        Enter value of N
        4
        The Circular Matrix is:
        1   2   3   4   
        12  13  14  5   
        11  16  15  6   
        10  9   8   7   
        

        示例用法 2:

        Enter value of N
        7
        The Circular Matrix is:
        1   2   3   4   5   6   7   
        24  25  26  27  28  29  8   
        23  40  41  42  43  30  9   
        22  39  48  49  44  31  10  
        21  38  47  46  45  32  11  
        20  37  36  35  34  33  12  
        19  18  17  16  15  14  13  
        

        【讨论】:

          猜你喜欢
          • 2013-03-13
          • 2012-01-13
          • 2014-12-12
          • 1970-01-01
          • 1970-01-01
          • 2013-01-11
          • 2012-03-17
          • 2014-09-26
          • 1970-01-01
          相关资源
          最近更新 更多