[英]Reference ArrayList object to objects of other ArrayLists
I have 3 different classes;我有 3 个不同的课程; Contestant, Event and Result.参赛者、事件和结果。
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:
}
The classes have different methods for adding new Contestant objects, new Event objects and new Result objects to each ArrayList.这些类有不同的方法来向每个 ArrayList 添加新的 Contestant 对象、新的 Event 对象和新的 Result 对象。 Each contestant can participate in multiple events and for each event they can create multiple results.每个参赛者可以参加多个事件,并且对于每个事件他们可以创建多个结果。
What I want to achieve is for each Result object to reference a Contestant ArrayList object as well as an Event ArrayList object - how can I best link them?我想要实现的是每个 Result 对象都引用一个 Contestant ArrayList 对象以及一个 Event ArrayList 对象 - 我怎样才能最好地链接它们?
Your event class should be like this ,instead of array list you can use Hashmap.你的事件类应该是这样的,你可以使用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;
}
And your result class should be like this:你的结果类应该是这样的:
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:
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.