java.lang.ArrayIndexOutOfBoundsException: 0 - 数组大于索引?
抛出的异常是否表明数组大于索引?如果不是,这意味着什么,为什么?我该如何纠正它?
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该数组不包含任何元素——它是一个空数组。因此,当您询问数组中的第一个元素(索引 0 处包含的元素)时,数组会显示“我在索引 0 处没有元素”。它通过抛出异常来“说明”这一点。在您的情况下,异常是 java.lang.ArrayIndexOutOfBoundsException: 0
这意味着您请求的索引超出了数组的范围。换句话说,数组有一个长度(它的边界)。当它的长度为 0(为空)并且您请求第一个元素时,该数组会告诉您您请求的项目不可用,因为该数组的长度甚至不是 1 个元素。
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.
这意味着数组小于索引。在这种情况下,这意味着数组为空。
您应该传递一个命令行参数以便在那里获得值。如果需要的话,你最好添加一些验证,比如
It means the array is smaller than the index. In that case it means the array is empty.
You should pass a command-line argument in order to have a value there. And if it is required, you'd better add some validation, like
这意味着它小于索引。换句话说,没有命令行参数,而您假设至少有一个。
It means that it is smaller than the index. In other words, there were no command line arguments, and you are assuming that there was at least one.
您正在访问不存在的数组索引。对于任何小于 0 或大于或等于数组长度的索引都会发生这种情况。
添加这个
You're accessing an index of the array which does not exist. This would happen for any index less than 0 or greater or equal to the length of the array.
Add this
抛出该异常表示已使用非法索引访问了数组。索引可以为负数,也可以大于或等于数组的大小。
这是对此的检查。
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
Here's a check against that.