Search code examples
javaruntime-errorindexoutofboundsexception

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

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)

Solution

  • You are getting an IndexOutOfBoundsException when you are trying to remove the duplicates from your array colors. This is because the remove() method of ArrayList can either take in an Object or an int and you are passing in an 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.

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