java - 如何修复ArrayList java.lang.IndexOutOfBoundsException : Index 20 out-of-bounds for length 2

标签 java runtime-error indexoutofboundsexception

我想在我的 arrayList“颜色”中存储没有配对的数字,但我的 arrayList 收到此运行时错误。 main 获取 n(数组大小)的输入和 arItems 的输入,然后将 arItems 中的元素转换为整数,将它们放入 int 数组 ar 中,然后当它调用 sockMerchant 时,它传递 int n 和 int 数组 ar

static int sockMerchant(int n, int[] ar) {
    ArrayList<Integer> colors = new ArrayList<Integer>(n);
    int pairs = 0;

    for (int i = 0; i < n; i++) {
       if (!colors.contains(ar[i])) {
            colors.add(ar[i]);
        } else {
            pairs++;
            colors.remove(ar[i]);
        }
    }

    System.out.println(pairs);
    return pairs;
}
private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {

    // n is the size of the array
    // sample n input: 9
    int n = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    int[] ar = new int[n];

    //sample arItems input: 10 20 20 10 10 30 50 10 20
    String[] arItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < n; i++) {
        int arItem = Integer.parseInt(arItems[i]);
        ar[i] = arItem;
    }

    int result = sockMerchant(n, ar);


    scanner.close();
}

我得到的错误是:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 20 out-of-bounds for length 2
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.remove(ArrayList.java:517)
    at Solution.sockMerchant(Solution.java:21)
    at Solution.main(Solution.java:48)

最佳答案

当您尝试从数组colors 中删除重复项时,您将收到IndexOutOfBoundsException。这是因为 ArrayListremove() 方法可以接受 Objectint,并且您可以传入一个int。这意味着您实际上正在尝试删除特定索引,在您的示例中是索引 20,但该索引在您的数组中不存在。

您可以修改代码,以根据索引正确删除值。

static int sockMerchant(int n, int[] ar) {
    ArrayList<Integer> colors = new ArrayList<Integer>(10);
    int pairs = 0;

    for (int i = 0; i < n; i++) {
       if (!colors.contains(ar[i])) {
            colors.add(ar[i]);
        } else {
            pairs++;
            colors.remove(color.indexOf(ar[i]));
        }
    }

    System.out.println(pairs);
    return pairs;
}

关于java - 如何修复ArrayList java.lang.IndexOutOfBoundsException : Index 20 out-of-bounds for length 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252714/

相关文章:

excel - VBA Mac Excel 2016 : Chart . 导出权限被拒绝错误 70

java - Split 方法抛出 IndexOutOfBounds 异常

java - 在 Eclipse 项目中混合使用 AspectJ 和 Scala

java - 使用 Java 构建插件系统的最佳方法

ios - RLMException : Can't persist property 'id' with incompatible type, 但不存在

java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 100

java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException :2

java - 如何使用 PortEx 从导入表中获取 DLL、函数名称和地址?

java - 为自定义按钮充气时发生 ClassNotFoundException(在手机上有效;在平板电脑上异常)

mysql - 当 N=0 时在 select 查询的限制子句中使用 N-1 时出现运行时错误