java.lang.ArrayIndexOutOfBoundsException: 0 - 数组大于索引?

发布于 2024-12-11 18:39:21 字数 630 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

儭儭莪哋寶赑 2024-12-18 18:39:21

该数组不包含任何元素——它是一个空数组。因此,当您询问数组中的第一个元素(索引 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.

苍暮颜 2024-12-18 18:39:21

这意味着数组小于索引。在这种情况下,这意味着数组为空。

您应该传递一个命令行参数以便在那里获得值。如果需要的话,你最好添加一些验证,比如

if (args.length == 0) {
    throw new IllegalArgumentException("year is required");
}

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

if (args.length == 0) {
    throw new IllegalArgumentException("year is required");
}
顾铮苏瑾 2024-12-18 18:39:21

这意味着它小于索引。换句话说,没有命令行参数,而您假设至少有一个。

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.

娇女薄笑 2024-12-18 18:39:21

您正在访问不存在的数组索引。对于任何小于 0 或大于或等于数组长度的索引都会发生这种情况。

添加这个

if(args != null || args.length < 1) {
  throw new IllegalArgumentException("Please provide an argument.");
}

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

if(args != null || args.length < 1) {
  throw new IllegalArgumentException("Please provide an argument.");
}
无人接听 2024-12-18 18:39:21

抛出该异常表示已使用非法索引访问了数组。索引可以为负数,也可以大于或等于数组的大小。

这是对此的检查。

public static void main(String[] args) { 
if (args.length != 1){
    System.out.println("Year Required")
    System.exit(0);
}
    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);
}

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.

public static void main(String[] args) { 
if (args.length != 1){
    System.out.println("Year Required")
    System.exit(0);
}
    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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文