简体   繁体   English

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5

I need help with a little array assignment that I am currently doing. 我需要当前正在做的一些数组分配方面的帮助。 So far I managed to correct alof of code but this one problem still remains.. 到目前为止,我设法纠正了全部代码,但是这一问题仍然存在。

public class AssignmentArray1 {
    public static void main(String[] args) {

        int a[][] = new int[ 10 ][ 5 ];

        for ( int i = 0; i < a.length; i++ )
        {
            for ( int j = 0; j < a[ i ].length; j++ ) {
                a[ j ][ i ] = j;
            }
        }
        for ( int i = 0; i < a.length; i++ )
        {
            for ( int j = 0; j < a[ i ].length; j++ )
                System.out.printf( "%d ", a[ j ][ i ] );
            System.out.println();
        }
    }

}

What's wrong with this? 这怎么了 I can't understand why I'm getting the error message 我不明白为什么会收到错误消息

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at assignmentarray1.AssignmentArray1.main(AssignmentArray1.java:25) 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:在atsignationarray1.AssignmentArray1.main(AssignmentArray1.java:25)处为5

So obviously something is wrong with a[ j ][ i ] = j; 因此显然a[ j ][ i ] = j; ? But exactly what is? 但是到底是什么?

It seems you have your indexes swapped by accident for the a array. 看来您不小心将索引换成了a数组。

This: 这个:

            a[ j ][ i ] = j;

Should be: 应该:

            a[ i ][ j ] = j;

And this: 和这个:

            System.out.printf( "%d ", a[ j ][ i ] );

Should be: 应该:

            System.out.printf( "%d ", a[ i ][ j ] );

The ending conditions in your two loops are the wrong way round. 您两个循环中的结束条件是错误的处理方式。 The easiest way to fix this is to change the assignment as follows: 解决此问题的最简单方法是如下更改分配:

            a[ i ][ j ] = i;

You also need to fix the second pair of nested loops. 您还需要修复第二对嵌套循环。

Change from, 从,

a[ j ][ i ] = j;

To, 至,

a[ i ][ j ] = j;

a.length = 10 and a[i].length=5 as per array declaration. 根据数组声明, a.length = 10a[i].length=5

Now, in this for loop 现在,在此for循环中

for ( int i = 0; i < a.length; i++ )
    {
        for ( int j = 0; j < a[ i ].length; j++ ) {
            a[ j ][ i ] = j;
        }
    }

as per array boundary 0<=i<10 and 0<=j<5 should be satisfied, but this a[ j ][ i ] = j; 按照数组边界0<=i<100<=j<5 ,但是a[ j ][ i ] = j; statement violates the boundary condition of the array because i can go upto 9 but allowed only in the range [0,5) so ArrayIndexOutOfBoundException is obvious. 该语句违反了数组的边界条件,因为i可以上升到9,但只允许在[0,5)范围内,所以ArrayIndexOutOfBoundException很明显。

Check your for loops and fix the indexing. 检查您的循环并修复索引。

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

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:8 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8 线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM