所以我有一个LeaderBoard对象的临时arraylist(如下所示),我想将所有具有相同游戏名称的玩家组合成一个新的arraylist。我该怎么做呢?
LeaderBoard [win=0, lose=1, gameName=QUFI, gamerTag=Ithroeann, platform=IO],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Ithroeann, platform=IO],
LeaderBoard [win=1, lose=0, gameName=CODE, gamerTag=Cirronith, platform=PS],
LeaderBoard [win=1, lose=0, gameName=CODE, gamerTag=Cirronith, platform=PS],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Deathstar, platform=IO],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Deathstar, platform=IO],
LeaderBoard [win=0, lose=1, gameName=QUFI, gamerTag=Deathstar, platform=IO],
LeaderBoard [win=0, lose=1, gameName=CODE, gamerTag=Mazzzap, platform=AN],
LeaderBoard [win=0, lose=1, gameName=NUPI, gamerTag=Mazzzap, platform=AN],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Mazzzap, platform=AN],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Redstripe, platform=PC],
LeaderBoard [win=0, lose=1, gameName=CODE, gamerTag=Redstripe, platform=PC],
LeaderBoard [win=1, lose=0, gameName=NUPI, gamerTag=Redstripe, platform=PC],
LeaderBoard [win=1, lose=0, gameName=CODE, gamerTag=Redstripe, platform=PC],
LeaderBoard [win=1, lose=0, gameName=QUFI, gamerTag=Crayonbreath, platform=PS],
LeaderBoard [win=1, lose=0, gameName=CODE, gamerTag=Quinesia, platform=XB],
LeaderBoard [win=1, lose=0, gameName=NUPI, gamerTag=Quinesia, platform=XB],
LeaderBoard [win=0, lose=1, gameName=QUFI, gamerTag=VolcanoBrawler, platform=PC],
LeaderBoard [win=0, lose=1, gameName=NUPI, gamerTag=VolcanoBrawler, platform=PC],
LeaderBoard [win=0, lose=1, gameName=NUPI, gamerTag=VolcanoBrawler, platform=PC],
LeaderBoard [win=0, lose=1, gameName=CODE, gamerTag=VolcanoBrawler, platform=PC],
LeaderBoard [win=0, lose=1, gameName=QUFI, gamerTag=CastleRock, platform=XB],
LeaderBoard [win=1, lose=0, gameName=NUPI, gamerTag=TorpedoBear, platform=PC],
LeaderBoard [win=1, lose=0, gameName=NUPI, gamerTag=GreenPanda, platform=AN],
LeaderBoard [win=0, lose=1, gameName=NUPI, gamerTag=GreenPanda, platform=AN]]
成:
LeaderBoard [win=1, lose=1, gameName=QUFI, gamerTag=Ithroeann, platform=IO]
LeaderBoard [win=2, lose=0, gameName=CODE, gamerTag=Cirronith, platform=PS]
等...
我无法弄清楚如何做到这一点。
答案 0 :(得分:1)
我可以用一些伪代码启动你吗?
// create an empty output array list
// for each element in the input array list
// attempt to add it to the output
// if the output already contains an element for this gamer tag, game name, etc
// update that element, adding to the win/loss count
// else
// add a new record with this win/loss count
EDIT1:好的,稍微扩大:
ArrayList<LeaderBoard> output = new ArrayList<LeaderBoard>();
:outer
for (LeaderBoard i : input) {
for (LeaderBoard o : output) {
if (i.gameName.equals(o.gameName)
&& i.gamerTag.equals(o.gamerTag)
&& i.platform.equals(o.platform)) {
o.win += i.win;
o.lose += i.lose;
break outer;
}
}
output.add(i);
}
return output;
EDIT2:如果您不想使用标记的中断:
ArrayList<LeaderBoard> output = new ArrayList<LeaderBoard>();
for (LeaderBoard i : input) {
boolean shouldAdd = true;
for (LeaderBoard o : output) {
if (i.gameName.equals(o.gameName)
&& i.gamerTag.equals(o.gamerTag)
&& i.platform.equals(o.platform)) {
o.win += i.win;
o.lose += i.lose;
shouldAdd = false;
break;
}
}
if (shouldAdd) {
output.add(i);
}
}
return output;
答案 1 :(得分:1)
你可以像Luke建议的那样手动完成(好的答案,+ 1)。
或者您可以创建LeaderBoard对象的HashMap,其中键是gamerTag,值是当前的LeaderBoard对象。
普通的HashMaps只允许每个键一个值,这样可以非常快速地查找,并且易于更新。