[Java中的“数组索引超出界限”是什么意思? [重复]

问题描述 投票:-1回答:1
public class UseArgument
{
    public static void main(String[] args)
    {
        System.out.print("Hi, ");
        System.out.print(args[0]);
        System.out.println(". How are you?");
    }
}

嗨,伙计们。在终端中执行它后,我从终端得到了一个报价,说:>

" Hi, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at UseArgument.main(UseArgument.java:6)"

有人可以向我解释这里出了什么问题吗?我只是在搜索Array Index Out Of Bounds Exception的意思,但是对此我仍然感到困惑。

public class UseArgument {public static void main(String [] args){System.out.print(“ Hi,”); System.out.print(args [0]); System.out.println(“。你好吗?”); }} ...

java indexoutofboundsexception
1个回答
0
投票

顾名思义,您正在访问数组之外​​的数组。运行main方法时,您将收到一个字符串数组-这些是您的代码传递的外部变量,它们初始化为args数组。您正在尝试访问该数组的第一个索引,如果执行时没有任何变量传递给您的程序,则该数组将为空,并且您将收到此异常。

© www.soinside.com 2019 - 2024. All rights reserved.