简体   繁体   English

似乎无法修复“ java.lang.ArrayIndexOutOfBoundsException”错误

[英]Can't seem to fix my 'java.lang.ArrayIndexOutOfBoundsException' error

I'm unsure if the problem in my program is simply in where I declared the arrays, or if I'm stuffing one of them with more than they can take. 我不确定程序中的问题仅仅是在声明数组的地方,还是我要在其中一个数组中塞满更多数组。

Here is my code: 这是我的代码:

import java.util.Scanner;
import java.util.Arrays;

public class EliminateDuplicates {
public static void main(String[] args) {

    int[] numberList = new int[10];

    System.out.println("Please enter ten integers.");

    Scanner input = new Scanner(System.in);

    for (int i = 0; i < numberList.length; i++) {

        numberList[i] = input.nextInt();

        }

    int[] newNumberList = new int[10];

    newNumberList = eliminateDuplicates(numberList);


    System.out.println("The discrete numbers are: ");

    for (int i = 0; i < newNumberList.length; i++) {

            System.out.println(newNumberList[i] + ' ');

        }

    }

public static int[] eliminateDuplicates(int[] numberList) {

    int[] noDuplicateList = new int[numberList.length];

    int size = 0;

    java.util.Arrays.sort(numberList);

    for (int i = 0; i < numberList.length; i++) {

        if (numberList[i] != numberList[i+1]) {

            noDuplicateList[i] = numberList[i];

            size++;
        }

    }

    return noDuplicateList;
}

} }

This is the output + the error message I get: 这是输出+我收到的错误消息:

Please enter ten integers.
9
8
7
3
3
4
1
2
8
6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at EliminateDuplicates.eliminateDuplicates(EliminateDuplicates.java:51)
at EliminateDuplicates.main(EliminateDuplicates.java:28)

Your problem is here : 您的问题在这里:

 for (int i = 0; i < numberList.length - 1; i++) {
     if (numberList[i] != numberList[i+1]) {

When i reaches numberList.length-1 , i+1 is out of the array bounds. i达到numberList.length-1i+1超出了数组范围。

You should change the range of the loop. 您应该更改循环范围。 You shold also use the size index when assigning to the noDuplicateList list, otherwise you will have gaps in that list. 分配给noDuplicateList列表时,您还要使用size索引,否则该列表中将有空白。

for (int i = 0; i < numberList.length - 1; i++) {
    if (numberList[i] != numberList[i+1]) {
        noDuplicateList[size] = numberList[i];
        size++;
    }
}

You would still have a minor problem. 您仍然会有一个小问题。 Since your eliminateDuplicates methods returns an array of the same size as the input, if there were duplicates, the output array would have unused indices in its end (that will contain zeroes). 由于您的eliminateDuplicates方法返回的数组大小与输入的大小相同,因此,如果存在重复项,则输出数组的末尾将有未使用的索引(将包含零)。 If you wish to avoid that, you can add all the items of the input array to a HashSet , find the size() of that set, and create an array of that size for the output. 如果希望避免这种情况,可以将输入数组的所有项目添加到HashSet ,找到该集合的size() ,然后为输出创建一个具有该大小的数组。 Using a Set will also simplify your code, since the Set would eliminate the duplicates for you, and you don't have to sort the input array. 使用Set还将简化您的代码,因为Set会为您消除重复项,并且您不必对输入数组进行排序。

public static int[] eliminateDuplicates(int[] numberList) {

    Set<Integer> noDups = new HashSet<Integer>();

    for (int num : numberList)  
        noDups.add(num);

    int[] noDuplicateList = new int[noDups.size()];

    Iterator<Integer> iter = noDups.iterator();
    for (int i=0; i<noDuplicateList.length && iter.hasNext();i++)
         noDuplicateList[i]=iter.next();

    return noDuplicateList;
}

您的问题在这里, eliminateDuplicates

if (numberList[i] != numberList[i+1]) {// i+1, for the last elements raise the exception
if (numberList[i] != numberList[i+1])

i=9它将查找超出范围的索引10

the problem is this line: 问题是这一行:

if (numberList[i] != numberList[i+1]) {

If i is equals numberList.length the second condition is out of bounds. 如果i等于numberList.length则第二个条件超出范围。

So you have to change: 因此,您必须更改:

for (int i = 0; i < numberList.length; i++) {

TO: 至:

for (int i = 0; i < numberList.length-1; i++) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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