當寫這樣簡單的東西:有人熟悉Netbeans:爲什麼會發生這種情況?錯誤不是一個真正的錯誤?
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter array size: ");
int x = sc.nextInt();
int [] anArray;
int index = 100;
anArray = new int[x];
for (int i=0; i<=x; i++){
anArray[i] = index;
index += 100;
System.out.println ("Element at index "+ i + ": " + anArray[i]);
}
}
}
Netbeans的編譯和正常運行的代碼,但在輸出端這樣看:
run:
Please enter array size:
12
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
Element at index 10: 1100
Element at index 11: 1200
at Practice.main(Practice.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
這似乎離我..爲什麼是例外通過代碼中途拋出?然後在最後完成?
它指向第21行: anArray [i] = index;
老實說不是一個大問題..我只是玩弄和審查一些Java的基礎知識(這是一段時間......)和例外是讓我覺得我做錯了什麼事,米不知道我其實是因爲它似乎工作我的意圖。
謝謝!
該問題處理更多的線程的同步,然後它與您的代碼。 Netbeans可能使用兩個不同的線程監視stderr和stdin,然後嘗試將輸出同步到單個線程(EDT)中,但是由於每條線(可能)都單獨同步,所以它們會重疊。是的,這很煩人。有人可能會爭辯說,有兩個獨立的窗戶會更有幫助,有時它會,有時它不會 – MadProgrammer