简体   繁体   中英

Why am I getting “ArrayIndexOutOfBoundsException: 0”?

The problem seems to be within my for loop when I am trying to place the value of temp into int[] unDupe at the i position. I get the error:

'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at main.Main.main(Main.java:33)'

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] unDupe = {};
    System.out.print("Enter a number sequence (eg. '1 2 10 20'): ");
    String dupe = input.nextLine();
    String[] dupeSplit = dupe.split(" ");

    int temp;
    for(int i = 0; i < dupeSplit.length; i++){
        temp = Integer.parseInt(dupeSplit[i]);
        unDupe[i] = temp; // line 33
    }
}

You are declaring and initializing the unDupe array with int[] unDupe = {}; — however this gives it no elements; a zero sized array.

As soon as you try to index it with anything unDupe[i] = temp; you are exceeding the array size - your index is out of bounds. For an array to even have unDupe[0] the array would have to be at least size=1.

If you are un-duplicating the input your deDupe array has to be, at most , the same size as the input array dupeSplit , so you can declare int[] unDupe = new int[dupeSplit.length]; after you know the size of dupeSplit. Just declare deDupe later.

when you initialize unDupe as {} , you are creating it with length 0 . Therefore when you try to put elements in unDupe at index 0 (or greater), you get an out of bounds exception, because for an array (in java) you can only store things in indices 0...(length - 1) . Fix this by initializing unDupe after you have the size of the number sequence.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a number sequence (eg. '1 2 10 20'): ");
    String dupe = input.nextLine();
    String[] dupeSplit = dupe.split(" ");
    int[] unDupe = new int[dupeSplit.length];

    int temp;
    for(int i = 0; i < dupeSplit.length; i++){
        temp = Integer.parseInt(dupeSplit[i]);
        unDupe[i] = temp;
    }
}

Since you initialise unDupe to be an empty array with the ={} notation you can not write to element 0 of it (it has no memory available at all).

Instead try to put the initialisation of unDupe after the split and replace it with:

int[] unDupe = new int[dupeSplit.length];
 int[] unDupe = {};

is an array of 0 elements. Trying to access unDupe[i] where i = 0->dupeSplit.length will cause an out of bounds exception to be thrown. Ask yourself how can you access an index in an array if no space has been allocated to it yet? ie unDupe[0] -> ERROR because the size of unDupe is 0. No elements exist!

You may want to add this:

 int[] unDupe = new int[dupeSplit.length];

after this line

 String[] dupeSplit = dupe.split(" ");

知道dupeSplit的大小后,启动unDupe。

Do like this :

String[] dupeSplit = dupe.split(" ");
int[] unDupe = new int[dupeSplit.length];

Now unDupe has an initial size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM