我有3个不同的班级;参赛者,赛事和结果。
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添加新的Contestant对象,新的Event对象和新的Result对象。每个参赛者都可以参加多个活动,每个活动都可以创造多个结果。
我想要实现的是每个Result对象引用一个Contestant ArrayList对象以及一个Event ArrayList对象 - 我该如何最好地链接它们?
答案 0 :(得分: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:
}