简体   繁体   中英

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

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

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'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.
so modify your if condition by adding this line

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 see that you haven't checked that everywhere. Please add this condition whenever you are accessing such a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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