简体   繁体   中英

Issue when iterating through lists: IndexOutOfBoundsException in Java

I'm writing a program that applies many principles of computational linguistics. My problem at this moment is the following piece of code form a method that "flexibilizes two definitions". This is, it compares two different definitions of the same word, and in each definition empty or blank spaces will be added to later on work with the altered definitions (with blank spaces added). Say we have the following two definitions, defining the term "free fall".

1) Free fall descent  of a body subjected only to            the   action of  gravity.
2) Free fall movement of a body in        a    gravitational field under  the influence of gravity

There is a list of words called stoplist, which contains the words: "of", "a", "in", "to", and "under". After the process, each word in the definition that is also contained in the stoplist must correspond to a blank space OR another stoplist word of the other definition. So after executing such process, the previous definitions, represented in two different lists, should look like this:

1) Free fall descent  of a body ____ ____ subjected     only  to     the action    of gravity.
2) Free fall movement of a body in   a    gravitational field under  the influence of gravity.

The code I wrote to achieve this is the following:

[...]

String[] sList = STOPLIST.split(" ");  //this is the stoplist
String[] definition1 = defA1.split(" ");  //this is the array of words of the first definition
String[] definition2 = defA2.split(" ");  //this is the array of words of the second definition
List<String> def1 = new ArrayList<String>();  
List<String> def2 = new ArrayList<String>();
List<String> stopList = new ArrayList<String>();

for(String word : definition1){
    def1.add(word); //I transform arrays into lists this way because I used to think that using .asList() was the problem.
}
for(String word : definition2){
    def2.add(word);
}
for(String word : sList){
    stopList.add(word);
}

int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); //here mdef will have the value of the lenght of the shortest definition, and we are going to use the value of mdef to iterate later on.

for(int i = 0; i < mdef; i++){
   if (stopList.contains(def1.get(i))) {  //here I check if the first word of the first definition is also found in the stoplist.
        if (!stopList.contains(def2.get(i))) {  //If the word of def1 previously checked is in the stoplist, as well as the corresponding word in the second definition, then we won't add a " "(blank) space in the corresponding position of the second definition.
           def2.add(i , " "); //here I add that blank space, only if the stoplist word in def1 corresponds to a non-stoplist word in def2. Again, we do this so the stoplist word in def1 corresponds to a blank space OR another stoplist word in def2.
           if(mdef == def2.size())
               mdef++; //In case the shortest definition is the definition to which we just added spaces, we increment mdef++, because that space added increases the length of the shortest definition, and to iterate in this recenlty extended definiton, we have to increment the index with which we iterate.
        }
    } else if (stopList.contains(def2.get(i))) { //this else if does the same than the previous one, but checks for the second definition instead of the first one. And adds blanks to def1 instead of def2 if necessary.
        if (!stopList.contains(def1.get(i))) {
            def1.add(i , " ");
            if(mdef == def1.size())
                mdef++;
        }
    }
}

[...]

Now, if you analyze the code carefully, you will realize that not all words of the lengthiest list will be checked, given that we iterate ove the definitions using the lenght of the shortest definition as index. This is fine, the remainding words of the lenghtiest definitions don't have to be checked, they will correspond to null spaces of the other definition (in case the lists don't end up being of the same lenght after the addition of spaces, as the previous exaple shows).

Now, after the explanation, the problem is the following: after running the main class, which calls the method that contains the previous code, a runtime exceptions pops out:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at main2.main(main2.java:75)

I don't understand why it is finding any of the lists as "empty". I have tried to solve it in too many ways, I hope a I gave a good explanation.

It may help as a clue that if I assign mdef to the lengthiest size instead of the shortest, that is :

int mdef = (def1.size() >= def2.size()) ? def1.size() : def2.size();

the error changes to:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 15
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at asmethods.lcc.turnIntoFlex(lcc.java:55)
    at asmethods.lcc.calLcc(lcc.java:99)
    at main2.main(main2.java:73)' 

Where lcc is the class that contains the method turnIntoFlex that contains the piece of code I'm showing. The line 55 of "turnIntoFlex" corresponds to the first line of the loop, that is:

if (stopList.contains(def1.get(i))) { [...]

Comments: The values of defA1 and defA2 are the definitions, respectively. ie def1 and def2, initially, are lists in which each separate element is a word. I can't check if these lists are being populated by printing them because the indexoutofboundsexception pops at the very moment the loop starts. However, I do print the values of the sizes of mdef, def1.size() and def2.size(), and the values turn out to be 13, or 15, showing that no list is empty before the "for" loop starts.

The mdef++ was something I added recently, not to exactly to solve this specific problem, but the error has been popping since before I added the mdef++ part. As I explained, The intention is to increase mdef++ when the shortest list is extended (but only when the short list is extended) so we iterate through all the words of the short list, and not more.

One issue with your code is that when you increment mdef you do not check to see if it now exceeds the length of the other list.

For example, suppose def1 had 3 words and def2 had 4 words. mdef would start at 3. But then suppose you successively add two spaces to def1 and increment mdef twice to be 5. This now exceeds the length of def2 and will then cause an index out of bounds exception in the def2 else condition if you keep iterating up to 5.


Added later:

Another serious issue with your code (that I thought of later) is that when you add the space to a list (either def1 or def2 ) this shifts the indices of all of the subsequent elements up by 1. So, for example, if you add a space at spot 0 in def1 when i is 0, then on the next pass through the loop, having incremented i to 1, you will look at the same word in def1 that you looked at in the previous pass. This is probably the source of some of your exceptions (as it would lead to a continual loop until you exceed the length of the other list: problem #1 above).


To correct both of these issues, you would need to change your code to something like:

int i = 0;
int j = 0;
while (i < def1.size()  &&  j < def2.size()) {
    if (stopList.contains(def1.get(i)) && !stopList.contains(def2.get(j)))
        def2.add(j++, " ");
    else if (stopList.contains(def2.get(j)) && !stopList.contains(def1.get(i)))
        def1.add(i++, " ");
    ++i;
    ++j;
}

Note that you don't ned mdef any more in this implementation.

Man, I think I got it. I modified the code, but I hope you understand what I did:

static public void main(String[] argv) {
    String[] sList = "of a in to under".split(" ");
    String[] definition1 = "Free fall descent of a body subjected only to the action of gravity"
            .split(" ");
    String[] definition2 = "Free fall movement of a body in a gravitational field under the influence of gravity"
            .split(" ");
    List<String> def1 = new ArrayList<String>();
    List<String> def2 = new ArrayList<String>();
    List<String> stopList = new ArrayList<String>();

    for (String word : definition1) {
        def1.add(word);
    }
    for (String word : definition2) {
        def2.add(word);
    }
    for (String word : sList) {
        stopList.add(word);
    }

    int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // Shortest
                                                                            // length

    for (int i = 0; i < mdef; i++) {
        System.out.println(i);
        if (!stopList.contains(def1.get(i)) && !stopList.contains(def2.get(i))) {
            continue;
        }

        else if (stopList.contains(def1.get(i)) && stopList.contains(def2.get(i))) {
            continue;
        }

        else if (!stopList.contains(def1.get(i)) && stopList.contains(def2.get(i))) {
            def1.add(i, " ");
            mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // define mdef again
        }

        else if (stopList.contains(def1.get(i)) && !stopList.contains(def2.get(i))) {
            def2.add(i, " ");
            mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); // define mdef again
        }

    }

    for (String word : def1) {

        if (word.equals(" "))
            System.out.print("_ ");
        else
            System.out.print(word+" ");
    }

    System.out.println();

    for (String word : def2) {
        if (word.equals(" "))
            System.out.print("_ ");
        else
            System.out.print(word+" ");
    }           
}

Is this the exact code you're using? I just ran it and it worked fine, I used:

import java.util.*;

public class HelloWorld {

    public static void main(String []args) {
        String stoplist= "of a in to and under";
        String defA1 = "Free fall descent  of a body subjected only to            the   action of  gravity";
        String defA2 = "Free fall movement of a body in        a    gravitational field under  the influence of gravity";

        String[] sList = stoplist.split(" ");  //this is the stoplist
        String[] definition1 = defA1.split(" ");  //this is the array of words of the first definition
        String[] definition2 = defA2.split(" ");  //this is the array of words of the second definition
        List<String> def1 = new ArrayList<String>();
        List<String> def2 = new ArrayList<String>();
        List<String> stopList = new ArrayList<String>();

        for (String word : definition1) {
            def1.add(word); //I transform arrays into lists this way because I used to think that using .asList() was the problem.
        }
        for (String word : definition2) {
            def2.add(word);
        }
        for (String word : sList) {
            stopList.add(word);
        }

        int mdef = (def1.size() <= def2.size()) ? def1.size() : def2.size(); //here mdef will have the value of the lenght of the shortest definition, and we are going to use the value of mdef to iterate later on.

        for (int i = 0; i < mdef; i++) {
            if (stopList.contains(def1.get(i))) {  //here I check if the first word of the first definition is also found in the stoplist.
                if (!stopList.contains(def2.get(i))) {  //If the word of def1 previously checked is in the stoplist, as well as the corresponding word in the second definition, then we won't add a " "(blank) space in the corresponding position of the second definition.
                    def2.add(i , " "); //here I add that blank space, only if the stoplist word in def1 corresponds to a non-stoplist word in def2. Again, we do this so the stoplist word in def1 corresponds to a blank space OR another stoplist word in def2.
                    if (mdef == def2.size())
                        mdef++; //In case the shortest definition is the definition to which we just added spaces, we increment mdef++, because that space added increases the length of the shortest definition, and to iterate in this recenlty extended definiton, we have to increment the index with which we iterate.
                }
            } else if (stopList.contains(def2.get(i))) { //this else if does the same than the previous one, but checks for the second definition instead of the first one. And adds blanks to def1 instead of def2 if necessary.
                if (!stopList.contains(def1.get(i))) {
                    def1.add(i , " ");
                    if (mdef == def1.size())
                        mdef++;
                }
            }
        }

        for (String word : def1) {
            System.out.print(word+",");
        }

        System.out.println();

        for (String word : def2) {
            System.out.print(word+",");
        }
    }
}

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