java.lang.ArrayIndexOutOfBoundsException: 0 - 数组大于索引? [英] java.lang.ArrayIndexOutOfBoundsException: 0 - Array larger than Index?

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

问题描述

抛出的异常是否表示数组大于索引?如果不是,那是什么意思,为什么?我该如何纠正?

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?

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

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

推荐答案

该数组不包含任何元素——它是一个空数组.因此,当您请求数组中的第一个元素(包含在索引 0 处的元素)时,数组会显示我在索引 0 处没有元素".它通过抛出异常来说明"这一点.在您的情况下,异常是 java.lang.ArrayIndexOutOfBoundsException: 0

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

这意味着您请求的索引超出了数组的范围.换句话说,数组有一个长度(它的边界).当它的长度为 0(它是空的)并且您要求第 1 个元素时,该数组会告诉您您请求的项目不可用,因为该数组甚至不是 1 个元素的长度.

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