使用命令行参数显示数组索引超出范围的异常

时间:2014-01-20 01:39:54

标签: java arrays indexoutofboundsexception

当我尝试执行下面的代码时,它会显示数组索引超出范围的异常。

public class aaags {

    public static void main(String[] args) {
        System.out.println("The length of args is:"+ args.length);
        System.out.println("The value of args[0] is:"+ args[0]);
        System.out.println("The value of args[1] is:"+ args[1]);
    }
}

1 个答案:

答案 0 :(得分:0)

您应该检查args.length(或使用for ( : )

System.out.println("The length of args is:" + args.length);
for (int i = 0; i < args.length; i++)
    System.out.println("The value of args[" + i + "] is:" + args[i]);