我剛開始學習java。我使用Eclipse IDE和JDK 7我剛剛得知陣列,並試圖運行此代碼:如何在java中解決java.lang.ArrayIndexOutOfBoundsException?
public class Testproj {
public static void main(String[] args){
int[] values = new int[4];
values[1] = 10;
values[2] = 20;
values[3] = 30;
values[4] = 40;
System.out.println(values[1]);
System.out.println(values[2]);
System.out.println(values[3]);
System.out.println(values[4]);
}
}
但我得到這個編譯時錯誤:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Testproj.main(Testproj.java:8)
爲什麼我收到此錯誤我該如何消除它?
你甚至嘗試谷歌什麼'ArrayIndexOutOfBoundsException'是什麼意思?從[documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html):「拋出以指示已使用非法索引訪問數組。索引爲或者爲負數或者**大於或等於數組**的大小。「 – Pshemo
非常感謝。我忘了那個。在問之前我應該先研究一下。 – Elben