簡體   English   中英

將 ArrayList 對象引用到其他 ArrayList 的對象

[英]Reference ArrayList object to objects of other ArrayLists

我有 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 對象 - 我怎樣才能最好地鏈接它們?

你的事件類應該是這樣的,你可以使用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:
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM