将创建的对象从一个类传递到另一个类并添加到ArrayList?

时间:2017-01-12 10:08:49

标签: java object arraylist methods

我已经创建了一个函数,您可以在其中为参与者添加事件。

现在我想将创建的对象传递给参与者类中的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");
}

这是我的参与者课程:

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;

}
}

我的结果类:

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);
}


}

1 个答案:

答案 0 :(得分:0)

你在做什么

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

但我在Result Class中没有看到addResult()方法。你需要做的是

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

希望这会有所帮助