Java – ArrayIndexOutOfBoundsException when iterating through all elements of an array

How to handle this exception "ArrayIndexOutOfBoundsException"

public static void main(String [] arg) {
    int [] a=new int [64];
    for(int i=1;i<=a.length;i++){
        a[i]=i;
        System.out.println(i);
    }

}

Solution

The array index in Java starts at 0 and goes to array length – 1. So change the loop to for (int i = 0; I < a.length; I)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>