真的不明白这里出了什么问题吗?
数组超出范围只是一个简单的例外。
public class Days
{
public static void main (String args[])
{
String[] dayArray = new String [4];
{
dayArray [0] = "monday";
dayArray [1] = "tuesday";
dayArray [2] = "wednesday";
dayArray [3] = "Thursday";
dayArray [4] = "Friday";
try
{
System.out.println("The day is " + dayArray[5]);
}
catch(ArrayIndexOutOfBoundsException Q)
{
System.out.println(" invalid");
Q.getStackTrace();
}
System.out.println("End Of Program");
}
}
}
有人也有任何想法为什么不能运行吗?我收到错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Days.main(Days.java:14)
最佳答案
数组受创建限制。在您的示例中,它的大小为4个字段。
对于索引为0的数组,这意味着您可以访问以下字段:
dayArray [0] = "monday";
dayArray [1] = "tuesday";
dayArray [2] = "wednesday";
dayArray [3] = "Thursday";