0

I am trying to iterate through a linked list of objects, doing a comparison of one object that I want to add to the linkedlist with each objects in the linkedlist so that I can find out where to place this new object. I am doing a comparison with both object's get(0) index which is a string and using compareToIgnoreCase(). I am running into an error right after the print statement "looping through linkedlist":

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at FileManager.readData(Aclass.java:248)
        at Aclass.display(Aclass.java:86)
        at Aclass.<init>(Aclass.java:34)
        at Aclass.main(Aclass.java:190)

here is iteration:

while(x.hasNext()){
        System.out.println("looping through linklist");
        //LinkedList<String> obj=x.next(); PROBLEM HERE
        if(obj.get(0).compareToIgnoreCase(subData.get(0)) <0){
            data.add(c,subData);
            System.out.println("ADDED");
            c=0;
        }
c++; //keeps track of current index of object in linkedlist
}

alternate code:

while(x.hasNext()){
    System.out.println("looping through linklist");
    LinkedList<String> obj=x.next();
    if(obj.get(0).compareToIgnoreCase(subData.get(0)) <0){

        System.out.println("ADDED");

        break;
    }
    c++;
}

data.add(c-1,subData);
c=0;

original full code of method:

public LinkedList<LinkedList<String>> readData() {

    LinkedList<LinkedList<String>> data=new LinkedList<>();
    LinkedList<String> subData=new LinkedList<>();
    Iterator<LinkedList<String>> x = data.listIterator(0);

    int i=0;
    int c=0;
    int flag=0;
    BufferedReader br = null;
    String workingDir = System.getProperty("user.dir");

    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader(workingDir + "/Data.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            if(i==4){
                i=0;
                if(flag==0){
                    data.add(subData);
                               subData=new LinkedList<>();  
                    System.out.println("OK NO SORT");
                    flag=1;
                }
                else{
                    while(x.hasNext()){
                        System.out.println("looping through linklist");
                        LinkedList<String> obj=x.next();
                        if(obj.get(0).compareToIgnoreCase(subData.get(0)) <0){
                            //data.add(c,subData);
                            System.out.println("ADDED");
                            c=0;
                        }
                        c++;
                    }

                    subData=new LinkedList<>(); 
                    System.out.println("added subdata to main linkedlist");
                }
            }
            else {
                subData.add(sCurrentLine);
                System.out.println("adding to sublist");
                i++;
            }

        }

        data.add(subData);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();       
        }
    }
    return data;
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user2644819
  • 1,787
  • 7
  • 28
  • 41
  • Lot of questions on SO with this issue. java.util.ConcurrentModificationException – kosa Feb 12 '14 at 23:17
  • I know, i've looked and i tried to do some modifications like doing data.add() after the iteration, not in the middle but nothing changed. – user2644819 Feb 12 '14 at 23:18
  • Aclass.java:248 which line it is? I don't think it is line you marked as problem here. – kosa Feb 12 '14 at 23:20
  • 1
    You're modifying the list while iterating through it. It's doing exactly what the docs say will happen when you do that. – Brian Roach Feb 12 '14 at 23:21
  • If i comment out data.add(c,subData) i still receive the error. – user2644819 Feb 12 '14 at 23:23
  • @Nambari that is line 248, the //LinkedList obj=x.next(); PROBLEM HERE – user2644819 Feb 12 '14 at 23:28
  • 1
    I answered a very similar question not long ago. [Should explain what the problem is](http://stackoverflow.com/a/21647125/2071828). In those case you are adding but the argument is the same. P.S. never ever access a `LinkedList` by index. – Boris the Spider Feb 12 '14 at 23:36
  • @BoristheSpider I came upon that answer while searching but as i said before even if i remove the data.add() method while it is iterating it does not change the error. – user2644819 Feb 12 '14 at 23:38
  • I edited my code where i have data.add() outside of the iteration but it is still the same error. Also why is it bad to access a linkedlist by index? the add() method accepts an int for specifying the index in the linkedlist. – user2644819 Feb 12 '14 at 23:45
  • I updated my code to show the entire method, hope that helps. I really cannot figure this out. – user2644819 Feb 13 '14 at 00:17
  • 2
    There's still another call of `data.add(subData);` in the if block above. Step through your code in a debugger and you'll see. – SimonC Feb 13 '14 at 00:38
  • I just added that, it was not there earlier but i felt like it needed to be. The iteration is in a separate if statement from that of the one you are referring to so how would it effect it? – user2644819 Feb 13 '14 at 00:44
  • Because a `LinkedList` does not _have_ an index. It is a line of doubly linked nodes. To get an index the _entire_ `List` needs to be walked (from either end) to get to the correct node. This is `O(n)`. An indexed collection, such as an `ArrayList` stores elements by index so index access is `O(1)`. There are some good answers on this topic [here](http://stackoverflow.com/q/322715/2071828). – Boris the Spider Feb 13 '14 at 08:24
  • 1
    Separate or same if statement does not matter. You're still using the iterator `x`, and that becomes invalid once you modify the list. Any modifications done during the iteration must be done using the iterator; anything else is unsafe. – kiheru Feb 13 '14 at 08:24
  • The rule is very simple **no not access read operations on the `List` while looping**. This rules applies **however you loop**. If you want to `add` use `Iterator.add`. If you want to `remove` use `Iterator.remove`. This is where `LinkedList` shines (see my link above). You are doing **multiple** calls to `add`; you have only commented _one of them_. – Boris the Spider Feb 13 '14 at 08:26

0 Answers0