[英]ArayIndexOutOfBoundsException isn't working
我的代碼有問題:
import java.io.*;
import java.util.*;
public class goTooFar {
public static void main(String[] a){
int i =4;
int[] ar = new int[i];
int n = i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
try {
n = sc.nextInt();
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Now you have gone too far");
}
System.out.println("Enter " + n + " elements of an array: ");
for (i = 0; i <= n; i++) {
try {
ar[i] = sc.nextInt();
} catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Now you have gone too far");
}
System.out.println("Output Numbers: ");
for (i = 0; i <= n; i++) {
System.out.println(ar[i]);
}
}
}
}
我有這樣的輸入:
> Enter the size of an array:
7
> Enter 7 elements of an array:
1 4 2 5 3 9 0 7
但是,事實證明是這樣的:
>Output Numbers:
1
0
0
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at goTooFar.main(goTooFar.java:31)
我該怎么做才能解決此問題? 有人能幫我嗎? 非常感謝。 :)
您將數組的長度初始化為4:
int i =4;
int[] ar = new int[i];
因此,當您輸入更長的數組長度時,您將獲得ArrayIndexOutOfBoundsException。
您需要從用戶獲取長度后創建數組:
System.out.println("Enter " + n + " elements of an array: ");
ar = new int[n]; // initialize a new array
for(i = 0; i < n; i++){ // n is not a valid index for an array of length n
同時更改另一個循環的范圍
for(i = 0; i <= n; i++){
System.out.println(ar[i]);
}
至
for(i = 0; i < n; i++){
System.out.println(ar[i]);
}
您的問題是這部分for(i = 0; i <= n; i++)
數組以索引0開頭。因此,如果數組的大小類似於您的情況7,則最高索引將為6。(0,1,2,3,4,5,6)
您只需要循環來檢查i < n
。
另外,在向用戶詢問數組的大小之前,已將數組的大小初始化為4。 因此,您需要在輸入發生后使用數組進行初始化
更改
n = sc.nextInt();
至
n = sc.nextInt();
ar = new int[n];
同樣,for循環的輸出應在輸入循環之外System.out.println("Output Numbers: "); for(int i = 0; i <n; i++){ System.out.println(ar[i]); }
System.out.println("Output Numbers: "); for(int i = 0; i <n; i++){ System.out.println(ar[i]); }
System.out.println("Output Numbers: "); for(int i = 0; i <n; i++){ System.out.println(ar[i]); }
最終代碼如下:
int[] ar = null;
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
try{
n = sc.nextInt();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Now you have gone too far");
}
ar = new int[n];
System.out.println("Enter " + n + " elements of an array: ");
for(int i = 0; i < n; i++){
try{
ar[i] = sc.nextInt();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("Now you have gone too far");
}
}
System.out.println("Output Numbers: ");
for(int i = 0; i <n; i++){
System.out.println(ar[i]);
}
您的問題已通過此代碼解決...我僅使用您的代碼...
public class goTooFar {
public static void main(String[] a) {
int i = 0, n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
n = sc.nextInt();
int[] ar = new int[n]; // Problem 1 Fixed.
System.out.println("Enter " + n + " elements of an array: ");
for (i = 0; i < n; i++) { // Problem 2 fixed
ar[i] = sc.nextInt(); // Enhancement 1
}
System.out.println("Output Numbers: ");
for (i = 0; i < n; i++) {
System.out.println(ar[i]);
}
sc.close(); // Enhancement 2
}
}
您做錯事的清單...需要修改的地方...
默認情況下,使用4個元素初始化數組,然后要求用戶輸入所需的元素數。
每次for (i = 0; i <= n; i++)
...時都具有n + 1次迭代...而是for (i = 0; i < n; i++)
...
沒問題...增強功能...
**dangerous**
否則無需try-catch
... 在獲取變量n的值后聲明數組,並將for循環范圍從0更改為<n:
public static void main(String[] args)
{
int n = 0 ;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
try{
n = sc.nextInt();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Now you have gone too far");
}
int [] ar = new int[n]; //declare the array after getting input from user about how many numbers will he enter
System.out.println("Enter " + n + " elements of an array: "); // change both loops to run from 0 to < n
for(int i = 0; i < n; i++){
ar[i] = sc.nextInt();
}
System.out.println("Output Numbers: ");
for(int i = 0; i < n; i++){
System.out.println(ar[i]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.