为什么我在这里得到 IndexOutOfBoundsException?

Why am i getting IndexOutOfBoundsException here?

import java.util.*;

class two_strings_annagrams1 {
    public static boolean compute(String inp1, String inp2) {
        ArrayList<Character> hs = new ArrayList<Character>();
        for (int i = 0; i < inp1.length(); i++) {
            hs.add(inp1.charAt(i));
        }
        System.out.println(hs);
        for (int j = 0; j < inp2.length(); j++) {
            hs.remove(inp2.charAt(j));
        }
        System.out.println(hs);
        if (hs.size() == 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String inp = s.nextLine();
        String inp2 = s.nextLine();
        System.out.println(compute(inp, inp2));
    }
}

当我使用 ArrayList 或 LinkedList 时,我遇到了 IndexOutOfBounds 异常,但是当我使用 HashSet 时,代码工作正常。出现异常的原因是什么,如何解决?

我认为 hs.remove(inp2.charAt(j)) 解析为 remove(int), not remove(Character),因为编译器更喜欢将 char 扩展为 int 而不是将其装箱为 Character

如果你使用

hs.remove((Character) inp2.charAt(j))

它将消除歧义。