gpt4 book ai didi

java - Java中如何统计数据

转载 作者:行者123 更新时间:2023-11-30 06:55:09 25 4
gpt4 key购买 nike

我尝试通过以下代码计算数据并删除重复数据,但我收到此代码的错误消息。

有人可以帮助我吗?

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++;
}

}
}

这是让我感动的日志日志

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)

最佳答案

出现错误是因为您尝试将元素添加到列表中(我假设 G.savelike 是一个列表),同时使用迭代器迭代同一个列表。

这是不允许的。相反,为所有新元素创建另一个 tmpList,将它们添加到其中,并在迭代器循环完成后使用 addAll() 方法。

像这样:

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

关于java - Java中如何统计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com