[英]Java OOP not able to add specific element from arraylist to another arraylist
I have an arraylist named teamList, where I have a list of custom object named Team.我有一个名为 teamList 的 arraylist,其中我有一个名为 Team 的自定义 object 列表。
I want to find a specific object in this arrayList.我想在这个 arrayList 中找到一个特定的 object。 When I find this object, I want to use my constructor 'addPlayer' to Team, that has its own arrayList named 'playersList'.当我找到这个 object 时,我想使用我的构造函数“addPlayer”到 Team,它有自己的 arrayList,名为“playersList”。
I am able to find the team that I want to specifically find, however I am not able to use the 'addPlayer' constructor.我能够找到我想专门找到的团队,但是我无法使用“addPlayer”构造函数。
I am attempting to here:我试图在这里:
@FXML public void AddingPlayer() {
Player player = new Player(newPlayerTextfield.getText());
Object currVal = newPlayerDropdown.getValue();
for (Object t : teams.getTeamList()){
if (t.equals(currVal)){
System.out.println(teams.getTeamList().indexOf(t));
//t.addPlayer(player);
}
}
}
Also I wanted to use a for(i = 0; i < teams.size(); i++), but for some reason size() was highlighted in red, and with no error message.我也想使用 for(i = 0; i < teams.size(); i++),但由于某种原因 size() 以红色突出显示,并且没有错误消息。
Sorry for obvious mistakes, I am new to programming抱歉有明显的错误,我是编程新手
Instead of this:而不是这个:
for (Object t : teams.getTeamList()){
Try to do so:尝试这样做:
for (Team t : teams.getTeamList()){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.