java - 将 ArrayList 对象引用到其他 ArrayList 的对象

标签 java arraylist

我有 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/

相关文章:

java - 列表无法解析

java - List<String> stringList = new ArrayList<String>() 和 List<String> stringList = new ArrayList() 有什么区别?

java - 新 Jframe 关闭时更新 Jlist

java - 如何更新ArrayList?

java - 如何将现有的 Java 应用程序转换为 SYS V 服务(守护进程)

java - Android-如何让移动的屏幕对象扩展Button

java - JVM/JLS 中是否指定永远不会加载未使用代码路径中的类?

java - 如何在 android 4.4 及更高版本中以编程方式检查我是否打开了 gps?

java - 使用 JGraphXAdapter 改变边的颜色

java - 通过 JTable 从 ArrayList 中删除项目