1

I try count data and remove Duplicate data by the following code ,but I got an error message with this code.

Can anyone help me?

StructorRecords.java

public class StructorRecords {

public String recorusername;
public String id;
public String full_name;
public String recordimg;
public Integer count;}

counter.java

public static void orderbylike(StructorRecords data) {
    if (G.bollike) {
        G.savelike.add(data);
    } else {
        Iterator itr = G.savelike.iterator();
        int i = 1;
        while (itr.hasNext()) {
            StructorRecords SR = (StructorRecords) itr.next();
            if (SR.id.equals(data.id)) {
                data.count = SR.count + 1;
                G.savelike.set(i, data);
            } else {
                G.savelike.add(data);
            }
            i++;
        }

    }
}

this is logs that get me LOG

02-01 17:19:23.142 7587-9416/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err: java.util.ConcurrentModificationException
02-01 17:19:23.152 7587-9416/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
02-01 17:19:23.152 7587-9416/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.counter.orderbylike(counter.java:21)
02-01 17:19:23.152 7587-9416/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.loginActivity$11.run(loginActivity.java:460)
02-01 17:19:23.162 7587-9416/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.lang.Thread.run(Thread.java:856)
02-01 17:19:23.451 7587-9472/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err: java.util.ConcurrentModificationException
02-01 17:19:23.451 7587-9472/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
02-01 17:19:23.451 7587-9472/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.counter.orderbylike(counter.java:21)
02-01 17:19:23.461 7587-9472/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.loginActivity$11.run(loginActivity.java:460)
02-01 17:19:23.461 7587-9472/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.lang.Thread.run(Thread.java:856)
02-01 17:19:23.583 7587-9417/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err: java.util.ConcurrentModificationException
02-01 17:19:23.583 7587-9417/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
02-01 17:19:23.583 7587-9417/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.counter.orderbylike(counter.java:21)
02-01 17:19:23.583 7587-9417/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at sanjinsgr.limosoftwaregroup.ir.instasanj.loginActivity$11.run(loginActivity.java:460)
02-01 17:19:23.592 7587-9417/sanjinsgr.limosoftwaregroup.ir.instasanj W/System.err:     at java.lang.Thread.run(Thread.java:856)
1
  • Can you post error log please!So it will be easier Commented Feb 1, 2017 at 13:44

2 Answers 2

1

Error is there because you try to add element to the List (I assume G.savelike is a List), while you iterate through the same list with iterator.

It is not allowed. Instead make another tmpList for all new elements, add them in it and after iterator loop done use addAll() method.

Like that:

List<StructorRecords> tmpList = new ArrayList<StructorRecords> tmpList;
while (itr.hasNext()) {
        StructorRecords SR = (StructorRecords) itr.next();
        if (SR.id.equals(data.id)) {
            data.count = SR.count + 1;
            G.savelike.set(i, data);
        } else {
            // here put to temp list
            tmpList.add(data);
        }
        i++;
    }
    // add all new elements
    G.savelike.addAll(tmpList);
Sign up to request clarification or add additional context in comments.

1 Comment

BTW: List index starts from 0, not from 1, so it seems like code may fail on the last element when G.savelike.set(i, data); invoked...
0

You can use the Collections from java concurrency package which will provide fail safe iterator, If you use Collections from java.util package the iterators are of Fail fast type and will throw Concurrent modification exception whenever you iterate and try update the collection at the same time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.