简体   繁体   English

如何修复ArrayList java.lang.IndexOutOfBoundsException:长度为2的索引20越界

[英]How to fix ArrayList java.lang.IndexOutOfBoundsException: Index 20 out-of-bounds for length 2

I want to store the numbers with no pair in my arrayList "colors" but I get this runtime error with my arrayList. 我想将不成对的数字存储在我的arrayList“ colors”中,但是我的arrayList出现此运行时错误。 The main gets input for n(size of the array) and input for arItems then it converts the elements in the arItems into integers places them in an int array ar then when it calls the sockMerchant it passes the int n and the int array ar 主体获取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();
}

The error I get is: 我得到的错误是:

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)

You are getting an IndexOutOfBoundsException when you are trying to remove the duplicates from your array colors . 当您尝试从数组colors删除重复项时,您将获得IndexOutOfBoundsException This is because the remove() method of ArrayList can either take in an Object or an int and you are passing in an int . 这是因为ArrayListremove()方法可以接受Objectint而您要传入int This means that you are actually trying to remove a specific index which, in your example is the index 20, but this index doesn't exist in your array. 这意味着您实际上是在尝试删除特定的索引,在您的示例中为索引20,但是该索引在您的数组中不存在。

You can modify your code as such to remove the values correctly based upon index. 您可以这样修改代码,以根据索引正确删除值。

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

暂无
暂无

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

相关问题 Arraylist java.lang.IndexOutOfBoundsException:索引 3 超出长度 3 错误的范围 - Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围 - java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 如何解决线程“AWT-EventQueue-0”中的异常 java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围 - how to solve Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 收到此错误 java.lang.IndexOutOfBoundsException:索引 487 超出长度 487 的范围 - Getting this error java.lang.IndexOutOfBoundsException: Index 487 out of bounds for length 487 为什么我会收到“线程中的异常”main“java.lang.IndexOutOfBoundsException: IndexOutOfBoundsException: Index 0 out of bounds for length 0” - why do I get an “Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 ” java.lang.IndexOutOfBoundsException: index=3 越界 (limit=6, nb=4) - java.lang.IndexOutOfBoundsException: index=3 out of bounds (limit=6, nb=4) 如何解决“ java.lang.IndexOutOfBoundsException:索引:1,大小:1”的问题 - How to fix “java.lang.IndexOutOfBoundsException: Index: 1, Size: 1” problem 如何修复致命异常:java.lang.IndexOutOfBoundsException 索引:0,大小:1 - How to fix Fatal Exception: java.lang.IndexOutOfBoundsException Index: 0, Size: 1 java.lang.IndexOutOfBoundsException:无效的索引20,大小为20 - java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20 ArrayList的java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException for ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM