遍历数组的所有元素时出现 ArrayIndexOutOfBoundsException [英] ArrayIndexOutOfBoundsException when iterating through all the elements of an array

查看:50
本文介绍了遍历数组的所有元素时出现 ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理这个异常ArrayIndexOutOfBoundsException"我的代码:我创建了一个长度为 64 的数组,然后初始化每个索引,然后打印索引以确保我已填满所有索引,但它最多打印 63,然后给出异常!任何想法

how to handle this exception "ArrayIndexOutOfBoundsException" my code : I create an array of 64 length then I intialized every index then I print the indexes to make sure I am fulling all indexes but it prints up to 63 then gives the exception !! any idea

    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);
    }

}

推荐答案

Java 中的数组索引从 0 开始,到 array.length - 1.因此将循环更改为 for(int i=0;i<a.length;i++)

The array indexes in Java start from 0 and go to array.length - 1. So change the loop to for(int i=0;i<a.length;i++)

这篇关于遍历数组的所有元素时出现 ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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