2017-01-12 7 views
2

私は3つの異なるクラスを持っています。コンテスト、イベント、結果。他のArrayListsのオブジェクトへのArrayListオブジェクトの参照

public class Contestant { 

public static ArrayList<Contestant> allContestants = new ArrayList<>(); 

private int contestantId; 
private String firstName; 
private String lastName; 

public Contestant (int contestantId, String firstName, String lastName) { 
    this.contestantId = contestantId; 
    this.firstName = firstName; 
    this.lastName = lastName; 
} 

public class Event { 

public static ArrayList<Event> allEvents = new ArrayList<>(); 

private String eventName; 

public Event (String eventName) { 
    this.eventName = eventName; 
} 

public class Result { 

public static ArrayList<Result> allResults = new ArrayList<>(); 

private double result; 
private int attemptNumber; 

public Result (double result, int attemptNumber) { 
    this.result = result; 
    this.attemptNumber = attemptNumber: 
} 

のクラスは、各ArrayListに新しい出場者オブジェクト、新しいEventオブジェクトと新しい結果オブジェクトを追加するためのさまざまな方法があります。各参加者は複数のイベントに参加でき、各イベントごとに複数の結果を作成できます。

私が達成したいのは、各ResultオブジェクトがContestant ArrayListオブジェクトとEvent ArrayListオブジェクトを参照することです。どうすればそれらをリンクするのが最適でしょうか?

+0

あなたはすべての出場者とすべてのイベントの完全なArrayListのオブジェクトを参照したい理由、それは、単一のイベントまたは単一の競技者を参照するために細かいですその結果に対応する – PyThon

+0

'Result'の' ResultList'を作成して 'Event'に渡し、' Event'の 'EventList'を作成して' Contestent'に渡します。これらのクラスの外では 'Collection'の' CollectionList'を作ります。 –

答えて

2

あなたのイベントクラスは、配列リストではなく、Hashmapを使用することができます。

public class Event { 
//In this you can have contestantId as key and Event as value 
public static Map<String, Event> allEvents = new HashMap<String, Event>(); 

private String eventName; 

public Event (String eventName) { 
    this.eventName = eventName; 
} 

そして、あなたの結果クラスには、次のようにする必要があります:

public class Result { 
//In this you can have eventName as key and Result as value 
public static Map<String, Result> allResults = new HashMap<String, Result>(); 

private double result; 
private int attemptNumber; 

public Result (double result, int attemptNumber) { 
    this.result = result; 
    this.attemptNumber = attemptNumber: 
} 
+0

結果では、eventNameにアクセスすることができ、eventNameにはcontestantId – emme

関連する問題