【问题标题】:"For" loop issue at ArrayList method in JavaJava中ArrayList方法的“For”循环问题
【发布时间】:2020-08-10 12:03:34
【问题描述】:

我在 TestVolleyTeam.java 遇到问题。我无法让列表数组工作,我真的很困惑。我真的不知道如何用“for”循环分配列表,我可能在那里犯了一个错误。 该代码应该通过 TestVolleyTeam 类使用 VolleyPlayer 类和 VolleyTeam 类来显示玩家列表。谁能发现我犯的错误?

class VolleyPlayer {
   private String name;
   private String surname;
   private int flnum;
   private int height;

   public String getName() {
       return name;
   }

   public String getSurname() {
       return surname;
   }

   public int getFlnum() {
       return flnum;
   }

   public int getHeight() {
       return height;
   }

   public VolleyPlayer(String n, String sur, int fl, int h) {
       name = n;
       surname = sur;
       flnum = fl;
       height = h;
   }

   public VolleyPlayer(String n, String sur, int fl) {
       this(n, sur, fl, 185);
   }

   public VolleyPlayer(String n, String sur) {
       this(n, sur, 3);
   }

   public VolleyPlayer(String n) {
       this(n, "Brown");
   }
}





import java.util.ArrayList;

public class VolleyTeam {
   private String teamname;
   private String city;
   private int year;
   private ArrayList<VolleyPlayer> players;

   public String getTeamname() {
       return teamname;
   }

   public String getCity() {
       return city;
   }

   public int getYear() {
       return year;
   }

   public ArrayList<VolleyPlayer> getPlayers() {
       return players;
   }


   public void setTeamname(String newTeamname) {
       this.teamname = teamname;
   }

   public void setCity(String newCity) {
       this.city = city;
   }

   public void setYear(int newYear) {
       this.year = year;
   }

   public void setPlayers(ArrayList<VolleyPlayer> newPlayers) {
       this.players = players;
   }
}  






import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.ArrayList;

public class TestVolleyTeam {
   public static void main(String[] args) {

       VolleyTeam myObj = new VolleyTeam();


       String teamname = JOptionPane.showInputDialog(null, "What is the name of the team?", "Input");

       myObj.setTeamname(teamname);


       String city = JOptionPane.showInputDialog(null, "What is the city of the team?", "Input");

       myObj.setCity(city);


       String input = JOptionPane.showInputDialog(null, "What is the foundation year of the team?", 
                                                 "Input");
       int year = Integer.parseInt(input);
       myObj.setYear(year);


       myObj.getTeamname();
       myObj.getCity();
       myObj.getYear();


       VolleyPlayer first = new VolleyPlayer("Michael", "Scott", 1, 175);
       VolleyPlayer second = new VolleyPlayer("Jim", "Halpert", 2, 191);
       VolleyPlayer third = new VolleyPlayer("Dwight", "Schrute", 3, 189);
       VolleyPlayer fourth = new VolleyPlayer("Darryl", "Philbin", 4, 188);
       VolleyPlayer fifth = new VolleyPlayer("Andy", "Bernard", 5, 182);
       VolleyPlayer sixth = new VolleyPlayer("Oscar", "Martinez", 6, 173);
       VolleyPlayer seventh = new VolleyPlayer("Stanley", "Hudson", 7, 180);
       VolleyPlayer eighth = new VolleyPlayer("Kevin", "Malone", 8, 185);
       VolleyPlayer ninth = new VolleyPlayer("Creed", "Bratton", 9, 183);
       VolleyPlayer tenth = new VolleyPlayer("Toby", "Flederson", 10, 177);

       ArrayList<VolleyPlayer> vplist = new ArrayList<VolleyPlayer>();

       for (VolleyPlayer volleyPlayer : vplist) {
           vplist.add(volleyPlayer);
       }

       myObj.getPlayers();
       myObj.setPlayers(vplist);



       ArrayList<VolleyPlayer> vplist2 = volleyTeam.getPlayers();

       for (VolleyPlayer volleyPlayer : vplist2) {
           vplist2.add(volleyPlayer);

       }

       volleyTeam.getPlayers();
       volleyTeam.setPlayers(vplist2);

       JOptionPane.showMessageDialog(null, vplist2, teamname  + " " + city + ", "  + year, 
                                     JOptionPane.INFORMATION_MESSAGE);

   }
}

【问题讨论】:

  • 抱歉,它在第二个“for”循环处中断

标签: java for-loop arraylist


【解决方案1】:

启动时List为空,这段代码

for (VolleyPlayer volleyPlayer : vplist) {

试图循环一个空的List。我想我明白了你想要什么,并且可以通过类似的方式来实现

List<VolleyPlayer> vplist = new ArrayList<>(Arrays.asList(
        first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth));

那么您不需要循环将first - tenth 添加到您的vplist。请注意,我在施工时将其更改为钻石运算符&lt;&gt;。并使用Arrays.asList添加元素。

由于您必须使用for 循环,因此创建一个内联数组。喜欢,

for (VolleyPlayer volleyPlayer : new VolleyPlayer[] {
        first, second, third, fourth, fifth, sixth, seventh,
        eighth, ninth, tenth }) {

【讨论】:

  • 注意,现在,你可以只写 List vpList = List.of(first, second, ...);
  • 我需要用'for'循环来做,这是一个学校作业:(
猜你喜欢
  • 2019-07-26
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多