简体   繁体   English

索引越界,但无法弄清楚原因

[英]Index out of bounds, but can not figure out why

  1. Problem summary问题总结

Index becomes out of bounds at some point in my loop, but I can not find when this happens and why it does.索引在我的循环中的某个时刻变得越界,但我无法找到这种情况发生的时间和原因。 I'm very sorry if this sort of question sounds very beginnerish but I've genuinely been doing my best over the past few days to try and figure out why.如果这类问题听起来很新手,我很抱歉,但在过去的几天里,我一直在竭尽全力尝试找出原因。

  1. I've tried these things:我试过这些东西:

I have closely watched many iterations of the loop with the debugger and added some variables (the ones with the debug-and-company names) to check if it does what it's supposed to, if I need a -1 or something like that, have fixed other issues but this one remains.我用调试器仔细观察了循环的多次迭代,并添加了一些变量(带有调试和公司名称的变量)来检查它是否按照预期进行,如果我需要 -1 或类似的东西,有修复了其他问题,但这个问题仍然存在。 It used to do this:它曾经这样做:

在此处输入图像描述

instead of this:而不是这个:

chaineAL.set(i + k, null);

I figured that was the problem, that it was messing up with the amount of items in the list and all of that... I also added the lengthMatch condition to make sure it went into the right one.我认为这就是问题所在,它弄乱了列表中的项目数量以及所有这些......我还添加了 lengthMatch 条件以确保它进入正确的条件。 It feels like there is something probably painfully obvious that... just doesn't work.感觉好像有些东西可能很明显……就是行不通。 I feel like at somepoint lengthMatch stays 2, and occurences doesn't add up?我觉得 lengthMatch 在某个时候保持 2,并且出现次数不加起来? But that's just some feeling i got while looking at some iterations of the loop in the debugger.但这只是我在调试器中查看循环的某些迭代时的一些感觉。 j also is 0 in the beginning so I don't think it's adding one extra, and 1049 is way too far from 1024 for that to potentially be the issue I think. j 一开始也是 0,所以我不认为它会额外增加一个,而且 1049 与 1024 相差太远,我认为这可能是问题所在。

  1. Code and error messages代码和错误消息

Here is the full method for context:这是上下文的完整方法:

public boolean analyser(String chaineSource, String sequence, int seuil){
    int occurences = 0; //nombre de fois que la séquence apparaît dans la chaîne

    ArrayList<Character> chaineAL = new ArrayList<>();

    for (int i = 0; i < chaineSource.length(); i++) {
        chaineAL.add( (char)chaineSource.charAt(i) );
    }

    ArrayList<Character> chaineInvAL = new ArrayList<>();

    for (int i = chaineAL.size() - 1; i > 0; i--) {
        chaineInvAL.add(chaineAL.get(i));
    }

    /*
     ** Loop infernal consistant à chercher, puis supprimer si trouvée, la séquence puis la chercher à
     * nouveau pour compter ses occurences
     */
    //on cherche la correspondance
    int i = 0;
    int lengthMatch = 0;
    ArrayList<Character> AL;

    for (int x = 0; x <= 1; x++) { //pour éviter de répéter le même code 2x pour les deux AL
        if (x == 0) //commencer par celui-ci
            AL = chaineAL;
        else        //le même bain de sang pour la chaîne inversée
            AL = chaineInvAL;

        for (char c: AL) {
            if (c == sequence.charAt(0)) { //entrer si on trouve le premier caractère de la séquence
                for (int j = 0; j < sequence.length(); j++) {
                  //  int debogtest = i+j;
                    //char ddebug = chaineAL.get(i+j);
                    //char debug =  sequence.charAt(j);                     //comparer les caractères suivants
                    if ( chaineAL.get(i+j) == sequence.charAt(j) && lengthMatch != sequence.length()) {
                        lengthMatch++;
                    }                                    //↓ correspondance parfaite !
                    else if (chaineAL.get(i+j) != sequence.charAt(j) && lengthMatch == sequence.length()) {
                        occurences++;
                        for (int k = 0; k < sequence.length(); k++) { //supprimer la séquence
                            chaineAL.set(i + k, null);
                        }
                        lengthMatch = 0;
                    }
                    else { //la séquence ne correspond pas
                        lengthMatch = 0;
                        break;
                    }
                }
            }
            i++;
        }

    }

    return seuil <= occurences;
}

Sorry, my comments and everything is in French, it's easier for me.抱歉,我的评论和所有内容都是法语,这对我来说更容易。 I doubt they are interesting enough to deserve a translation, I tend to over comment.我怀疑它们是否足够有趣,值得翻译,我倾向于过度评论。 Since that looks like a bunch of weird code I'll explain what it does.由于那看起来像一堆奇怪的代码,我将解释它的作用。 Basically I'm looking for something in a long String.基本上我正在寻找一个长字符串中的东西。 In my test I'm looking for "12" (sequence) and I'm trying to find it 8 times (seuil).在我的测试中,我正在寻找“12”(序列)并且我试图找到它 8 次(seuil)。 I've come up with that array list system.我想出了那个数组列表系统。 I split the String in a list of chars, and every time I'll see the first char come up (in my "12" test it means every time it sees a "1"), it looks at the char right after it in the list.我将字符串拆分为一个字符列表,每次我看到第一个字符出现时(在我的“12”测试中,这意味着每次看到“1”时),它都会查看紧随其后的字符名单。 And the idea is that every time it does find it it wipes it from the list to search for it again etc. to count how many times it's found.这个想法是,每次它确实找到它时,它都会从列表中擦除它以再次搜索它等等以计算它被找到的次数。 the whole chaineInvAL is just the same one but reversed so that I can look from right to left too.整个 chaineInvAL 是同一个但颠倒了,这样我也可以从右向左看。

Here is what comes up when I try to run this:这是我尝试运行它时出现的情况: 例外 I don't know when the value of i becomes 1049, how it can be out of bounds.不知道什么时候i的值变成1049,怎么会越界。 I've tried thinking of it yet could not find what was the little breach in what I wrote that broke the logic.我试着想过它,但找不到我写的打破逻辑的小漏洞。

It points here:它指向这里: 行异常指向

Thank you so very much to those who will have the patience to look into this.非常感谢那些有耐心调查此事的人。

Before accessing the character at chaineAl.get(i+j), you need to check that i+j < chaineAl.size(), this might be the reason you are getting array index out of bound exception.在 chaineAl.get(i+j) 访问字符之前,您需要检查 i+j < chaineAl.size(),这可能是您获取数组索引超出范围异常的原因。
so modify your if condition by adding this line所以通过添加这一行来修改你的 if 条件

if ( i + j< chaineAl.size() && // your remaining conditions ) 

NOTE: Before accessing any summed indices ( like i+j ), it is always better to check if that value is less than the size of the list.注意:在访问任何求和索引(如 i+j )之前,最好检查该值是否小于列表的大小。 I see that you haven't checked that everywhere.我看到你没有到处检查。 Please add this condition whenever you are accessing such a value.每当您访问此类值时,请添加此条件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM