【问题标题】:Pass a created object from one class to another and add to ArrayList?将创建的对象从一个类传递到另一个类并添加到 ArrayList?
【发布时间】:2017-05-27 09:24:15
【问题描述】:

我创建了一个函数,您可以在其中为活动的参与者添加结果。

现在我想将创建的对象传递给我的参与者类中的 addResult 方法,然后将其添加到同一类中的 ArrayList 中,但我真的不知道该怎么做。我已经被这个问题困住了一段时间,可能需要一些帮助来进一步解决这个问题。

这是我迄今为止为此编写的代码:

    public Participant getParticipant() {
    int startNumber = readInt();

    boolean participantFound = false;

    for (int i = 0; i < allParticipants.size(); i++) {

        if (allParticipants.get(i).getStartNumber() == (startNumber)) {

            participantFound = true;
            return allParticipants.get(i);
        }

    }

    if (!participantFound) {

        System.out.println("No participant with number " + startNumber + " exists. " + "\n");
        runCommandLoop();
    }

    return null;
}

public Event getEvent() {
    String eventName = norm();

    boolean eventFound = false;

    for (int a = 0; a < allEvents.size(); a++) {

        if (allEvents.get(a).getEventName().equalsIgnoreCase(eventName)) {
            eventFound = true;

            return allEvents.get(a);


        }

    }

    if (!eventFound) {

        System.out.println("No event called " + eventName + " found! ");
        runCommandLoop();
    }

    else {

        System.out.println("To many attempts! ");
        runCommandLoop();
    }

    return null;

}

public void addResult() {

    System.out.println("Number: ");
    Participant p = getParticipant();

    if (p == null) {

    }

    System.out.println("Event: ");
    Event e = getEvent();

    if (e == null) {

    }

    System.out.println("Type in the result for " + p.getFirstName() + " " + p.getLastName() + " in "
            + e.getEventName() + ": ");

    double result = readDouble();

    while (result < 0) {
        System.out.println("Must be greater than or equal to zero! Try again: ");
        result = readDouble();

    }

Result r = new Result();

                          **// THIS IS WHERE I´M STUCK RIGHT NOW**

r.addResult();



    System.out.println("The result " + result + " is now registred");
}

这是我的 Participant 课程:

import java.util.ArrayList;

public class Participant {

public ArrayList<Result> allResults = new ArrayList<>();

private String firstName;
private String lastName;
private String team;
private int startNumber;

public Participant(String firstName, String lastName, String team, int startNumber) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.team = team;
    this.startNumber = startNumber;

}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getTeam() {
    return team;
}

public void setTeam(String team) {
    this.team = team;
}

public int getStartNumber() {
    return startNumber;
}

public void setStartNumber(int startNumber) {
    this.startNumber = startNumber;
}

public void addResult(Result r) {

    allResults.add(r);
    }





public String toString() {
    return "\n" + "Name: " + firstName + "\n" + "Last name: " + lastName + "\n" + "Team: " + team + "\n"
            + "Start number: " + startNumber;

}
}

还有我的 Result 类:

public class Result {

private double result;

public Result(double result) {


    this.result = result;


}

public double getResult() {
    return result;
}

public void setResult(double result) {
    this.result = result;

}


public void printResult() {
    System.out.println(result);
}


}

【问题讨论】:

    标签: java object arraylist methods


    【解决方案1】:

    你在做什么

    Result r = new Result();
    **// THIS IS WHERE I´M STUCK RIGHT NOW**
    r.addResult();
    

    但我在结果类中看不到addResult() 方法。你需要做的是

    p.addResult(r); // p is your Participant
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2015-07-12
      • 2017-12-20
      • 2021-12-10
      • 2013-04-12
      相关资源
      最近更新 更多