ArayIndexOutOfBoundsException不起作用

时间:2015-08-10 11:06:26

标签: java arrays exception-handling

我的代码存在问题:

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)

我该怎么做才能解决这个问题?有人能帮我吗?非常感谢。 :)

6 个答案:

答案 0 :(得分:1)

将数组的长度初始化为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]);
    }

答案 1 :(得分:0)

您的问题是这部分for(i = 0; i <= n; i++)

数组以索引0开头。因此,如果数组的大小与您的情况7相似,则最高索引将为6.(0,1,2,3,4,5,6)

您只需要循环来检查i < n

此外,在向用户询问数组大小之前,您已初始化大小为4的数组。因此,您需要在输入发生后使用数组进行初始化

答案 2 :(得分:0)

更改

n = sc.nextInt();

n = sc.nextInt();
ar = new int[n];

答案 3 :(得分:0)

循环的输出也应该在输入循环之外 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]);
    }

答案 4 :(得分:0)

使用此代码解决了您的问题...我只使用了您的代码...

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
    }
}

你做错的事情的列表......哪里需要做修改 ...

  1. 默认情况下,使用4个元素初始化数组,然后要求用户输入他想要的元素数量。

  2. 每次使用for (i = 0; i <= n; i++)时,您都会进行n + 1次迭代...而是使用for (i = 0; i < n; i++) ...

  3. 没有问题......增强功能......

    1. 除非您正在执行try-catch ...
    2. ,否则无需**dangerous**
    3. 永远关闭你的资源......这是一个很好的做法。

答案 5 :(得分:0)

在获取变量n的值后声明数组,并将for循环范围从0更改为&lt; 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]);
        }    
}