java.lang.ArrayIndexOutOfBoundsException:0 - 阵列比指数更大? [英] java.lang.ArrayIndexOutOfBoundsException: 0 - Array larger than Index?

查看:201
本文介绍了java.lang.ArrayIndexOutOfBoundsException:0 - 阵列比指数更大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抛出的异常是否表明该数组比该指数越大?如果没有,这是什么意思,为什么?如何纠正呢?


异常线程mainjava.lang.ArrayIndexOutOfBoundsException:0
    在leapyear.LeapYear.main(LeapYear.java:13)


 公共类LeapYear {公共静态无效的主要(字串[] args){
    年整型=的Integer.parseInt(参数[0]);
    布尔isLeapYear;    //被4整除
    isLeapYear =(年%4 == 0);    //能被4整除,而不是100
    isLeapYear = isLeapYear&放大器;&安培; (每年100%!= 0);    //被4整除,而不是100,除非被400整除
    isLeapYear = isLeapYear || (每年400%== 0);    的System.out.println(isLeapYear);
}
}


解决方案

该数组不包含任何元素 - 它是一个空数组。所以,当你问数组中的第一个元素(包含在索引0的元素)数组说:我没有在索引0的元素。它抛出异常'说'这一点。在你的情况,异常为 java.lang.ArrayIndexOutOfBoundsException:0

这意味着,您所要求的指标是数组的边界之外。换句话说,该阵列具有一个长度(它的边界)。当它的长度为0(它是空的),你问的第一个元素,数组告诉您请求的项目是不可用,因为该数组是连1元长。

Does the exception thrown indicate that the array is larger than the index? If not, what does it mean, and why? How do I correct it?


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at leapyear.LeapYear.main(LeapYear.java:13)


public class LeapYear {

public static void main(String[] args) { 
    int year = Integer.parseInt(args[0]);
    boolean isLeapYear;

    // divisible by 4
    isLeapYear = (year % 4 == 0);

    // divisible by 4 and not 100
    isLeapYear = isLeapYear && (year % 100 != 0);

    // divisible by 4 and not 100 unless divisible by 400
    isLeapYear = isLeapYear || (year % 400 == 0);

    System.out.println(isLeapYear);
}
}

解决方案

The array doesn't contain any elements--it is an empty array. So when you ask for the first element in the array (the element contained at index 0) the array says "I don't have an element at index 0". It 'says' this by throwing an exception. In your case, the exception is java.lang.ArrayIndexOutOfBoundsException: 0

This means that the index you requested is outside the bounds of the array. In other words, the array has a length (it's bounds). When it's length is 0 (it's empty) and you ask for the 1st element, the array tells you the item you requested is unavailable because the array isn't even 1-element long.

这篇关于java.lang.ArrayIndexOutOfBoundsException:0 - 阵列比指数更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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