我有一个ArrayList(Result),其中Result对象都持有对Event对象的引用。ArrayList中可能有50个Result对象,但只有5个不同的Event对象。是否有一种方法可以遍历ArrayList并将具有相同Event.getName()引用的Results分组在一起?
我想要分别在事件“高跳”中运行方法,然后仅在事件“跳远”等中运行方法。 我事先不知道结果列表中包含哪些事件,因为它们是从用户输入创建的。
我知道如何按事件名称对ArrayList进行排序,但我希望将列表按事件拆分并将它们存储在其他临时列表中(?)
来自Result类:
以上是我想在原始结果列表中运行所有事件的内容。
我想要分别在事件“高跳”中运行方法,然后仅在事件“跳远”等中运行方法。 我事先不知道结果列表中包含哪些事件,因为它们是从用户输入创建的。
我知道如何按事件名称对ArrayList进行排序,但我希望将列表按事件拆分并将它们存储在其他临时列表中(?)
来自Result类:
public Result(double result, int attemptNumber, Participant participant, Event event) {
this.result = result;
this.attemptNumber = attemptNumber;
this.participant = participant;
this.event = event;
}
来自事件类:
public Event (String eventName, int attemptsAllowed) {
this.eventName = eventName;
this.attemptsAllowed = attemptsAllowed;
}
public String getEventName() {
return eventName;
}
结果对象以未排序形式存储在名为allResults的ArrayList中。以下是第三个类中的一个方法,该方法基于输入的事件名称对所有结果进行排序和修剪,并仅保留每个参与者的最高结果(称为resultByEvent):
public ArrayList<Result> resultsByEvent(String eventName) {
resultsByEvent.addAll(allResults);
for(int i = 0; i < resultsByEvent.size(); i++) {
Event event = resultsByEvent.get(i).getEvent();
if(!event.getEventName().equals(eventName)) {
resultsByEvent.remove(i);
}
}
Collections.sort(resultsByEvent, new EventLeaderboard());
for(int n = 0; n < resultsByEvent.size(); n++) {
for(int j = n + 1; j < resultsByEvent.size(); j++) {
Participant participant1 = resultsByEvent.get(n).getParticipant();
Participant participant2 = resultsByEvent.get(j).getParticipant();
if(participant1.getParticipantId() == participant2.getParticipantId()) {
resultsByEvent.remove(j);
j = j - 1;
}
}
}
return resultsByEvent;
}
以上是我想在原始结果列表中运行所有事件的内容。
Result
和Event
的代码? - bradimusjava-8
吗? - Chetan Kinger