通过数组的所有元素时ArrayIndexOutOfBoundsException异常 [英] ArrayIndexOutOfBoundsException when iterating through all the elements of an array

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

问题描述

如何处理这个异常ArrayIndexOutOfBoundsException异常
我的code:我创建了64长度的数组,然后我intialized各项指标,然后我打印索引,以确保我缩绒的所有索引,但它最多可打印63则给出例外!任何想法

 公共静态无效的主要(字串[] ARG){
    INT [] A = INT新[64];
    的for(int i = 1; I< =则为a.length;我++){
        一个[我] =我;
        的System.out.println(ⅰ);
    }}


解决方案

在Java中开始的数组索引从 0 并转到 array.length - 1 。所以循环改为的for(int i = 0; I<则为a.length;我++)

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

}

解决方案

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天全站免登陆