我在cmd中遇到这个错误,但在intellij中它工作正常当我输入数据3次时就会发生这种情况

dgiusagp  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(212)

数组列表创建和方法(当我运行displaypremierleaguetable()方法时发生)。它在intelij中运行,当我在cmd中编译并运行它时,就会发生这种情况。

private ArrayList<FootBallClub> sportsClubsList= new ArrayList<>();

添加成员方法在此处输入图像描述

@Override
     public void AddNewFootBallClub (FootBallClub Cmember) {
        if(Cmember instanceof FootBallClub ){
            sportsClubsList.add(Cmember);
            System.out.println();
            System.out.println(Cmember.getName()+ " successfully added!");

        }else if (Cmember instanceof SclFootBallClub){
            sportsClubsList.add(Cmember);
            System.out.println();
            System.out.println(Cmember.getName()+ " successfully added!");
        }
        else if(Cmember instanceof UniversityClub){
            sportsClubsList.add(Cmember);
            System.out.println();
            System.out.println(Cmember.getName() + " successfully added!");
        }
        else{
            System.out.println("Full");
        }
    }

显示表格

@Override
        public void DisplayPremierLeagueTable () {

            int x = sportsClubsList.size();
            FootBallClub footBallClub= null;
            for (int i = 0; i<x;i++){
                for(int j = 1; j<(x-i);j++){
                    if((sportsClubsList.get(j-1)).getPoints()<((sportsClubsList.get(j)).getPoints())){
                        footBallClub = sportsClubsList.get(1-j);
                        sportsClubsList.set(1-j, sportsClubsList.get(j));
                        sportsClubsList.set(j, footBallClub);
                    }
                }
            }

                String Name = "Name of the Club";
                String Reg_id = "Registration Id";
                String Location = "Location";
                String Cap_Name = "Name of Captain";
                String Member_Count = "Member Count";
                String Num_of_Wins = "Wins";
                String Num_of_Defeats = "Defeats";
                String Num_of_Draws = "Draws";
                String Num_of_Matches = "Matches";
                String Goal_Soc = "Scored Goals";
                String Goal_received = "Received Goals";
                String Points = "Points";
                System.out.println("                                       ******************************Premier League Table******************************           ");
                System.out.println(" __________________________________________________________________________________________________________________________________________________________");
                System.out.printf(" |%-15s|%-15s|%-15s|%-15s|%-10s|%-10s|%-10s|%-10s|%-15s|%-15s|%-10s|\n", Name, Reg_id, Cap_Name, Member_Count, Num_of_Wins, Num_of_Defeats, Num_of_Draws, Num_of_Matches, Goal_Soc, Goal_received, Points);
                System.out.println(" __________________________________________________________________________________________________________________________________________________________");
            for (FootBallClub CMember : sportsClubsList) {
                System.out.printf(" |%-16s| %-14s| %-14s| %-14s| %-9s| %-10s| %-9s| %-9s| %-12s| %-13s |  %-10s|\n", CMember.getName(), CMember.getRegistration_Number(), CMember.getCaptain_Name(), CMember.getMember_Count(), CMember.getNum_of_Wins(), CMember.getNum_of_defeats(), CMember.getNum_of_draws(), CMember.getNum_of_Matches(), CMember.getNum_of_Goals_Scored(), CMember.getNum_of_Goals_Received(), CMember.getPoints());
                System.out.println(" __________________________________________________________________________________________________________________________________________________________");
            }

            }

输入cmd时出错

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
            at java.util.ArrayList.elementData(ArrayList.java:424)
            at java.util.ArrayList.get(ArrayList.java:437)
            at PremierLeagueManager.DisplayPremierLeagueTable(PremierLeagueManager.java:62)
            at MainCli.userInput(MainCli.java:41)
            at MainCli.displayMenu(MainCli.java:18)
            at MainCli.AddNewFootballClub(MainCli.java:253)
            at MainCli.userInput(MainCli.java:35)
            at MainCli.displayMenu(MainCli.java:18)
            at MainCli.userInput(MainCli.java:51)
            at MainCli.displayMenu(MainCli.java:18)
            at MainCli.AddNewFootballClub(MainCli.java:253)
            at MainCli.userInput(MainCli.java:35)
            at MainCli.displayMenu(MainCli.java:18)
            at MainCli.AddNewFootballClub(MainCli.java:253)
            at MainCli.userInput(MainCli.java:35)
            at MainCli.displayMenu(MainCli.java:18)
            at Tester.main(Tester.java:4)*

我没有给数组列表任何成员计数。

xnifntxz

xnifntxz1#

你好像在整理这些东西 sportsClubsList 通过交换球棒 j 以及 1-j . 在我看来你是想交换 j 以及 j-1 相反。
p、 在java中,命名约定是方法以小写字母开头

相关问题