Java IndexOutOfBounds,当它不应该

Java IndexOutOfBounds,当它不应该,java,Java,我正在学习Java语言,我正在尝试制作智囊团游戏。 我在尝试编译时遇到以下错误,在尝试调试代码后,我找不到错误: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 97, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.remove(ArrayList.java:474

我正在学习Java语言,我正在尝试制作智囊团游戏。 我在尝试编译时遇到以下错误,在尝试调试代码后,我找不到错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 97, Size: 4
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.remove(ArrayList.java:474)
    at mastermind.Combinacion.aciertos(Combinacion.java:115)
    at mastermind.Combinacion.principal(Combinacion.java:36)
    at mastermind.MasterMind.main(MasterMind.java:21)
Java Result: 1
下面的代码是方法“aciertos”,它给出一个密钥和一个组合,告诉您密钥中有多少字母

    public int aciertos(Combinacion comb){
    int aciert = 0;
    List<Character> clave = new ArrayList();
    // Transform the key to a List, so we can easily check what contains and remove elements
    for(char c : this.codigo){
        clave.add(c);
    }
    // We go though the user try, and we check if the letter it is in the secret key list. If it is, we remove it from the list, to avoid counting twice if the user has a letter repeated and it is only once in the secret key.
    for (int i = 0; i < comb.codigo.length; i++) {
        if(clave.contains(comb.codigo[i])){
            aciert++;
            clave.remove(comb.codigo[i]);
        }
    }
    return aciert;
}
public-int-aciertos(组合梳){
int aciert=0;
List clave=new ArrayList();
//将键转换为列表,这样我们就可以轻松地检查包含哪些元素并删除这些元素
for(char c:this.codigo){
添加(c)段;
}
//我们通过用户的尝试,检查该字母是否在密钥列表中。如果是,我们将其从列表中删除,以避免在用户重复某个字母且该字母在密钥中仅出现一次时计数两次。
for(int i=0;i
以下是Combinacion类中的字段:

//Size of each combination
private int tamano = 4;
//Los valores válidos son: blanco, negro, azul, rojo, verde, marron
private static Character[] valoresValidos = {'b', 'n', 'a', 'r', 'v', 'm'};
private static List<Character> valores = Arrays.asList(valoresValidos);
private char[] codigo = new char[tamano];
//每个组合的大小
塔马诺私人酒店=4;
//洛斯瓦洛雷斯·瓦利多斯之子:布兰科、黑人、蓝精灵、罗霍、维德、马伦
私有静态字符[]valoresValidos={'b','n','a','r','v','m'};
私有静态列表valores=Arrays.asList(valoresValidos);
private char[]codigo=新char[tamano];

p.D:我应该开始用英语写和评论所有东西,很抱歉用西班牙语写。

第一行的错误是,您试图在一个只有4个元素的ArrayList中读取索引97
ArrayList.remove()
使用索引作为参数,而不是要删除的对象:

clave.remove(comb.codigo[i])
必须替换为:

clave.remove(clave.indexOf(comb.codigo[i]))

我认为
clave.remove(com.codigo[I])
char
参数被提升为
int
,而不是装箱为
字符。最后调用的是
ArrayList#remove(int-index)
,而不是您想要的方法(
ArrayList#remove(Object o)

尝试手动装箱
字符
,如下所示:

for (int i = 0; i < comb.codigo.length; i++) {
    if(clave.contains(comb.codigo[i])){
        aciert++;
        clave.remove(Character.valueOf(comb.codigo[i]));
    }
}
for(int i=0;i
似乎clave.remove()行使用comb.codigo[i]的内容作为索引。本例中使用的值为97,对应于小写字母“a”。这就是数组包含的内容吗?

Combinacion.java的第115行是什么?
codigo的类型是什么
char[]
?在
Combinacion
类中有两个名为
tamano
的字段@balwinder singh clave.remove(comb.codigo[i])@劳尔,你已经有答案了。试试看,不删除与数组位置相同的列表元素吗?我想从列表中删除数组中I位置的第一个元素。这就是我的代码所做的。indexOf返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回-1。您是对的,我不是一眼就知道的。两个答案都很好,我接受了另一个答案,这对我来说更容易理解,但我也愿意接受你的答案。谢谢@Raul,没问题。我不明白为什么索引是97!。实际上,它是char的值(在本例中为“a”)。这就解决了,谢谢。