3
這裏是我的代碼:'爲' 增強循環導致一個ArrayIndexOutOfBoundsException
import java.util.Scanner;
public class Arrays {
public static void main(String[] args) {
Arrays psvm = new Arrays();
psvm.start();
}
public void start() {
Scanner ben = new Scanner(System.in);
int[] arr = new int[4];
int[] arrs = new int[4];
for (int i = 0; i < arr.length; i++) {
arr[i] = ben.nextInt();
}
check(arr, arrs);
}
public void check(int arr[], int arrs[]) {
for (int i = 0; i < arr.length; i++) {
arrs[i] = arr[i];
}
for (int i : arrs) {
System.out.println(arrs[i]);
}
}
}
增強for
循環給人ArrayIndexOutOfBoundsException
:
for (int i : arrs) {
System.out.println(arrs[i]);
}
雖然這for
循環語句的作品。爲什麼?代碼有什麼問題?
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}