我有 3 个不同的类(class);参赛者、项目和结果。
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 对象。每个参赛者可以参加多个 Activity ,并且对于每个 Activity ,他们可以创造多个结果。
我想要实现的是让每个 Result 对象引用 Contestant ArrayList 对象以及 Event ArrayList 对象 - 如何最好地链接它们?
最佳答案
你的事件类应该是这样的,你可以使用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:
}
关于java - 将 ArrayList 对象引用到其他 ArrayList 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41610535/