2017-01-12 2 views
2

3 가지 클래스가 있습니다. 참가자, 이벤트 및 결과. 다른 ArrayList의 객체에 대한 ArrayList 객체 참조

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를 새로운 참가자 목적, 새로운 이벤트 객체와 새로운 결과 객체를 추가하기위한 다른 방법을


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

. 각 참가자는 여러 이벤트에 참여할 수 있으며 각 이벤트마다 여러 결과를 만들 수 있습니다.

달성하려는 목표는 각 Result 객체가 Contestant ArrayList 객체와 Event ArrayList 객체를 참조하는 것입니다. 어떻게 연결하는 것이 가장 좋습니까?

+0

, 그것을 잘 단일 이벤트 또는 단일 참가자를 참조하는 것입니다 그 결과에 해당 – 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; 
} 

그리고 당신의 결과 클래스는 다음과 같이해야합니다 :

모든 참가자 및 모든 이벤트의 전체 ArrayList의 객체를 참조 할 이유
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

관련 문제