私はResultオブジェクトがすべてEventオブジェクトへの参照を保持するArrayList(Result)を持っています。 ArrayListにはおそらく50のResultオブジェクトがありますが、5つの異なるEventオブジェクトしかありません。 ArrayListをループしてグループ化する方法はありますか同じEvent.getName()参照を持つ結果はありますか?複合オブジェクトのリストをグループ化します
「ハイジャンプ」イベントで結果にメソッドを実行し、次に「ロングジャンプ」イベントでのみ結果を返すようにしたい 結果リストが保持するイベントは、あらかじめわかりませんユーザー入力から作成されます。
私はイベント名でのArrayListをソートする方法を知っているが、私はむしろ、イベントでリストを分割し、他の一時リストに保存したい
結果クラスからは(?):
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に呼ばallResultsにソートされていない保存されていますresultByEvent)入力eventNameのに基づいており、参加者ごとにのみ、最高の結果を保持します:
上記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;
}
は、私は、元の結果リストのすべての私のイベントで実行したいものです。
をあなたが投稿できます'Resのコードイベントとは? – bradimus
いくつかのコードを共有してください。 – nullpointer
あなたは 'java-8'ですか? – CKing