简体   繁体   English

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

[英]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.

相关问题 将对象添加到ArrayLists的ArrayList - Adding an object to an ArrayList of ArrayLists 为什么在一个 ArrayList 中改变一个对象会在所有其他 ArrayList 中改变它? - Why does mutating an object in one ArrayList change it in all other ArrayLists? 如何访问通用ArrayLists的ArrayList中的对象 - How to access the objects inside an ArrayList of generic ArrayLists 使用双精度类型对对象的数组列表的数组列表进行排序 - Sorting an arraylist of arraylists of objects using double type 如何将Arraylist分配给其他2个Arraylist? - How do I distribute an Arraylist to 2 other Arraylists? Java 8流:将ArrayLists中的两个对象的属性组合为第三种对象类型的ArrayList - Java 8 streams : Combine properties of two objects which are in ArrayLists into an ArrayList of a third object type 如何引用将对象添加到对象内部的ArrayList - How to reference adding objects to ArrayLists inside of objects JAVA使用对象填充临时Arraylist,然后将其添加到由arrayLists组成的ArrayList中 - JAVA fill a temporary Arraylist with objects and then add it into a ArrayList which consists of arrayLists 序列化包含其他ArrayList对象的ArrayList对象的问题 - Issues Serializing an ArrayList object containing other ArrayList objects ArrayList的ArrayList - ArrayList of ArrayLists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM