繁体   English   中英

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

[英]Pass a created object from one class to another and add to ArrayList?

我创建了一个函数,可以在其中添加事件参与者的结果。

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

}
}

还有我的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);
}


}

你在做什么

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

但我在Result Class中看不到addResult()方法。 您需要做的是

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

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM