对象的Arraylist为空?

时间:2015-04-22 21:03:39

标签: java object arraylist

所以在这个类中选择== 1,它不会通过对象的ArrayList进入for循环,它是否意味着对象是空的?因为我在开头声明了对象并将它们添加到ArrayList存储中。

public static void main(String[] args) {
        ArrayList<Team> store = new ArrayList<>();
        Random gener = new Random();
        String tourName , tourDate , location;
        int maxNumberofTeams , avalSoft, avalHard , avalFieldTest;
        Scanner input = new Scanner(System.in);
        System.out.print("Please Enter tournament Name?\n");
        tourName = input.next();
        System.out.print("please Enter tournament Date\n");
        tourDate = input.next();
        System.out.print("please Enter location\n");
        location = input.next();
        System.out.print("Please Enter Max number of Teams\n");
        maxNumberofTeams = input.nextInt();
        System.out.print("Please Enter avalSoft\n");
        avalSoft = input.nextInt();
        System.out.print("Please enter aval Hard\n");
        avalHard = input.nextInt();
        System.out.print("Please Enter avalFieldTest\n");
        avalFieldTest = input.nextInt();
        Tournament tour = new Tournament (tourName , tourDate, location , maxNumberofTeams, avalSoft , avalHard, avalFieldTest);
        for (int i = 1 ; i <= maxNumberofTeams ; i++)
        {
            String teamName , sponsoringSchool , financialSponsor , judgeLocation;
            int teamNumber , noOfTeamMem , robotId;
            System.out.print("Please Enter %s team Name\n");
            teamName = input.next();
            System.out.print("Number of Team Memebers\n");
            noOfTeamMem = input.nextInt();
            System.out.print("Please Enter Sponsoring Schoolr\n");
            sponsoringSchool = input.next();
            System.out.print("Please Enter financialSponsor\n");
            financialSponsor = input.next();
            System.out.print("Please Enter judge Location\n");
            judgeLocation = input.next();
            teamNumber = i;
            System.out.print("Please Enter an ID for robot\n");
            robotId = input.nextInt();
            Robot robbb = new Robot();
            Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation, robbb);
            Robot rob = new Robot (team , robbb);
            store.add(team);
        }
        int choice=0;
        while(choice >= 0)
          {
            System.out.print("MENU\n"
                    + "1)PREPARING TEAM TURN ROBOT ON\n"
                    +"2)PREPARING TEAM HARDWARE\n"
                    + "3)HAVE HW INSPECT READY ROBOT\n"
                    + "4)HAVE PRERPARING TEAM TAKE HW_INSPECTED ROBOT TO STATION\n"
                    + "5)HAVE SW INSPECT ROBOT\n"
                    + "6)HAVE A PREPARING TEAM TAKE TO FIELD TEST\n"
                    + "7)HAVE A FIELD TEST INSPEC\n"
                    + "8)HAVE A BEFORE TEAM GO TO JUDGE\n"
                    + "9)HAVE JUDGES INTERVIEW\n"
                    + "10)CHANGE TEAM STATUS TO PASSED_INSPECTION\n"
                    + "11)TOURNAMENT STATUS TO MATCH\n"
                    + "12)\n"
                    + "13)ROBOT CAN'T PLAY IF OFF, READY, OR STILL AT TESTING\n"
                    + "14)CHANGE TOURNAMENT TO MATCHES\n"
                    + "15)GENERATE POINTS\n"
                    + "16)JUDGE POINTS\n"
                    + "17)CHANGE TOURNAMENT TO AWARDS\n"
                    + "18)PRINT TOP TEAMS JUDGING\n"
                    + "19) PRINT TOP BY QULIFYING \n"
                    + "20)DISPLAY TEAM PERSONS\n"
                    + "21)DISPLAY TEAM INFO\n"
                    + "22)DISPLAY INFO ABOUT ROBOTS\n"
                    + "23)INFO ABOUT TOURNAMENT\n"
                    + "24)END\n");

            choice = input.nextInt();
            Team temp = new Team();
            Robot robottemp = new Robot();
            Tournament tourr = new Tournament();
            if (choice == 1)
            {
                System.out.print("Phase"+choice);
                for (int i = 0 ; i >= store.size(); i++)
                {
                    System.out.print("Phase"+choice);
                    store.get(i).teamStatus = temp.teamStatus.PREPARING;
                    if (store.get(i).teamStatuss == 1)
                    {
                     store.get(i).robot.robotStatusChoice(1);
                     System.out.print("PHASE 1 COMPLETED\n");
                    }
                }
            }

这是我称之为方法的机器人类;

public int teamNumber;
    public int robotId;
    public Robot robot;
    Random gener = new Random();
    public int robotStatusChoice;
    ArrayList stations = new ArrayList();
    public int Height;
    public int Width = 22;
    public int Depth;
    public Team TeamAssigned = new Team("TEAMNAME", 1 , 1, "SS", "FS", "JL",robot);
    public robotStatus robotStatus;

    public Robot()
    {

    }
    public Robot( Team TeamAssigned ,Robot  robot){
        this.robot = robot;
        this.TeamAssigned = TeamAssigned;
    }


public void robotStatusChoice(int i) {
            if (i == 1)
            {
                this.robotStatusChoice = i;
                this.robotStatus = robotStatus.READY;
            }
            else if( i == 2)
            {
                this.robotStatusChoice = i;
                this.robotStatus = robotStatus.HW_INSP_PASSED;
            }
            else if ( i == 3)
            {
                this.robotStatusChoice = i;
                this.robotStatus = robotStatus.FIELD_TEST_PASSED;
            }
            else if ( i > 3 || i < 1)
            {
                this.robotStatusChoice = i;
                this.robotStatus = robotStatus.READY;
            }
        }

TeamSTatus Change;

public void TeamStatus(int x)
    {
        if( x == 1 )
        {
            this.teamStatuss = x;
            this.teamStatus = teamStatus.PREPARING;
        }
        else if ( x == 2)
        {
            this.teamStatuss = x;
            this.teamStatus = teamStatus.PASSED_INSPECTION;
        }
        else if ( x == 3 )
        {
            this.teamStatuss = x;
            this.teamStatus = teamStatus.PLAYED5_MATCHES;
        }
        else if ( x == 4)
        {
            this.teamStatuss = x;
            this.teamStatus = teamStatus.INELIGIBLE;
        }
        else if ( x > 4 || x<1)
        {
            this.teamStatuss = x;
            this.teamStatus = teamStatus.INELIGIBLE;
        }


    }

3 个答案:

答案 0 :(得分:1)

在最后的choice == 1块中,您的for循环条件会向后移动。当条件为for而不是true时,false循环的下一次迭代会发生。扭转你的状况。

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

答案 1 :(得分:1)

这是问题所在:

for (int i = 0 ; i >= store.size(); i++)

除非store为空(在这种情况下,您将获得例外),i >= store.size()将立即成为false。你的意思是:

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

或者更好的是,使用增强的for循环:

for (Team team : store) {
    team.teamStatus = temp.teamStatus.PREPARING;
    if (team.teamStatuss == 1) {
        team.robot.robotStatusChoice(1);
        System.out.print("PHASE 1 COMPLETED\n");
    }
}

(你真的有teamStatusteamStatuss作为字段吗?这对我来说听起来不太好。)

答案 2 :(得分:0)

for (int i = 0 ; i >= store.size(); i++)

这可能是一个错字,因为你可能意味着i < store.size()

在您的代码中,您有:

Robot robbb = new Robot();
Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation, robbb);
Robot rob = new Robot (team , robbb);

现在,您正在将未正确实例化的Robot()robbb)传递给Team的构造函数。创建setRobot(Robot robot)方法,将Team的机器人更新为robRobot,在添加Team之前引用Team } List

public void setRobot(Robot rob) { this.robot = rob; };