Java二维数组抛出边界

Java二维数组抛出边界,java,arrays,Java,Arrays,我对数组不熟悉 我创建的这个程序对BlueJays类中的每个方法都抛出了一个越界错误 老实说,我甚至不确定我在数组中的搜索是否正确。任何帮助都将不胜感激 谢谢 这是我的主要任务 public class Lab1 { static final int NBPLAYERS = 11; static final int NBMONTHS = 6; public static void main(String[] args) { String[] month = {"April", "M

我对数组不熟悉

我创建的这个程序对BlueJays类中的每个方法都抛出了一个越界错误

老实说,我甚至不确定我在数组中的搜索是否正确。任何帮助都将不胜感激

谢谢

这是我的主要任务

public class Lab1 {

static final int NBPLAYERS = 11;
static final int NBMONTHS = 6;

public static void main(String[] args) {

    String[] month = {"April", "May","June", "July", "August","September"};
    String[] players = {"Colabello","Donaldson","Smoak","Martin","Goins","Encarnacion","Carrera","Tulowitzki","Pillar","Bautista","Travis"};
    double[][] battingAvg = {
                    {0,.368,.300,.224,.386,.268},
            {.319,.306,.269,.287,.324,.296},
            {.229,.310,.213,.191,.203,.262},
            {.197,.327,.239,.256,.138,.213},
            {.276,.236,.172,.240,.314,.279},
            {.205,.225,.303,.241,.407,.279},
            {0,.302,.282,.244,.333,.231},
            {0,0,0,.357,.214,.237},
            {.273,.181,365,.283,.240,.323},
            {.164,.295,.226,.219,.286,.293},
            {.325,.189,.313,.368,0,0}};

    double [][] onBase = {
                    {.417,.330,.286,.413,.362,.429},        
            {.370,.373,.322,.370,.408,.403},
            {.372,.333,.275,.283,.243,.324},
            {.367,.362,.329,.322,.263,.300},
            {.323,.278,.221,.286,.442,.347},
            {.258,.333,.382,.384,.460,.411},
            {0,.357,.333,.277,.333,.313},
            {0,0,0,.400,.325,.250},
            {.297,.237,.380,.323,.283,.363},
            {.325,.418,.388,.300,.370,.436},
            {.393,.246,.313,.421,0,0}};


    PlayerStats Player;

    BlueJays team = new BlueJays(NBPLAYERS, NBMONTHS);

    team.getHighestSingleMonthBattingAvg();

    team.getHighestOnBase(5);

    team.getLowestBattingAvg(6);

    team.getBestMonth("Bautista");

    team.getBestOverallRecord();

    team.getLowestOnBase();
}
}
这是我的PlayerStats,它有访问器/变异器方法

class PlayerStats {

private String name;
private int month;
private double battAvg, onBase;


public PlayerStats(String name, int month, double battingAvg, double onBase2) {

    this.name = name;
    this.month = month;
    this.battAvg = battingAvg;
    this.onBase =  onBase2;

}

public String getName() {

    return name; 
}

public void setName(String name) {

    this.name = name;
}

public int getMonth() {

    return month;
}

public void setMonth(int month) {

    this.month = month; 
}


public double getAvg() {

    return battAvg;
}

public double getOnBase() {

    return onBase;
}

public double getBoth() {

    return battAvg + onBase;
}

}
这是创建2d数组的类

class BlueJays {

int nbPlayers;
int nbMonths;
int j = 0;
int highestBattingAvg;
int highestBattingMonth;
String highestBattingPlayer;
int highestOnBase;
int lowestAvg;
String lowestAvgPlayer;
int highestOverall;
String highestOverallPlayer;
int lowestOnBase;
int lowestOnBaseMonth;
String highestOnBasePlayer;
double bestOverAllMonth;



public BlueJays(int nbplayers2, int nbmonths2) {
    this.nbPlayers = nbplayers2;
    this.nbMonths = nbmonths2;

}

    PlayerStats[][] stats = new PlayerStats[nbPlayers][nbMonths];

PlayerStats setPlayerStatsCell(PlayerStats player, int iPlayer, int iMonth) {
    return player;


}


PlayerStats getHighestSingleMonthBattingAvg() {

    while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][j].getAvg();


            if(highestBattingAvg < stats[i][j].getAvg()) {
                highestBattingMonth = stats[i][j].getMonth();
                highestBattingPlayer = stats[i][j].getName();

            }

            if (i == nbMonths) {
                j++;
                i = 0;

            }

            System.out.println("Highest average batting player for the month " + highestBattingMonth + " is " + highestBattingPlayer);

        }
    }


    return null;


}

PlayerStats getHighestOnBase(int month) {

    while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][month].getOnBase();

            if(highestOnBase < stats[i][month].getOnBase()) {

                highestOnBasePlayer =  stats[i][month].getName();
            }

            if (i == nbMonths) {
                j++;
                i = 0;

            }
            System.out.println("Highest average onBase player for the month " + month + highestOnBasePlayer);

        }
    }


    return null;

}


public PlayerStats getLowestBattingAvg(int month) {

    while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][month].getOnBase();

            if(lowestAvg > stats[i][month].getAvg()) {

                lowestAvgPlayer =  stats[i][month].getName();
            }

            if (i == nbMonths) {
                j++;
                i = 0;

            }

            System.out.println("Lowest average batting player for the month " + month + " is " + lowestAvgPlayer);

        }
    }
    return null;

}

   PlayerStats getBestMonth(String player) {

   while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][j].getBoth();

            if(bestOverAllMonth > stats[i][j].getAvg() && stats[i][j].getName().contains(player)) {


                 bestOverAllMonth =  stats[i][j].getBoth();


            }

            if (i == nbMonths) {
                j++;
                i = 0;

            }
            System.out.println("Best month for the player " + player + " is " + bestOverAllMonth);


        }
    }
    return null;






}

public String getBestOverallRecord() {

    while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][j].getBoth();

            if(highestOverall < stats[i][j].getBoth()) {

                highestOverallPlayer =  stats[i][j].getName();
            }

            if (i == nbMonths) {
                j++;
                i = 0;

            }

            System.out.println("Highest overall record is " + highestOverallPlayer);

        }
    }
    return null;



}



public PlayerStats getLowestOnBase() {

    while(j < nbMonths) {

        for(int i = 0; i < nbPlayers; i++)  {
            stats[i][j].getOnBase();

            if(lowestOnBase > stats[i][j].getOnBase()) {

                double lowestOnBase =  stats[i][j].getOnBase();

                if(lowestOnBase > 0) {

                    lowestAvgPlayer = stats[i][j].getName();

                } else {

                    i++;
                }
            if (i == nbMonths) {
                j++;
                i = 0;

            }

        }
            System.out.println("Lowest On Base is  " + lowestOnBase);


    }



}
    return null;

}
}

你的问题可能在于:

// ... lots of other stuff
public BlueJays(int nbplayers2, int nbmonths2) {
     this.nbPlayers = nbplayers2;
     this.nbMonths = nbmonths2;
}

PlayerStats[][] stats = new PlayerStats[nbPlayers][nbMonths];
如果初始化
BlueJay
-类的实例,它将在设置值
nbPlayers
nbMonths
之前创建数组。所以您将有一个大小为0和0的数组

要避免这种情况,您必须执行以下操作:

// ... lots of other stuff
public BlueJays(int nbPlayers, int nbMonths) {
    this.nbPlayers = nbPlayers;
    this.nbMonths = nbMonths;
    // doesn't matter if you take the parameters or fields
    this.stats = new PlayerStats[nbPlayers][nbMonths];
}

希望我能帮到你:)

我不知道例外情况是什么,但下面是我通过扫描你的代码得到的结果

  • 您从不重置
    j
    ,因此大多数函数调用都无法通过第一次比较
  • 函数调用
    team.getLowestBattingAvg(6)
    使用索引6,但由于
    nbMonths=6
    ,因此可以使用的最高索引是5(索引从0到5,包括0到5)

  • 同时发布异常在设置nbPlayers&nbMonth的值之前,您正在初始化数组,因此数组大小为0。它发生在第一个数组上,其中J为零。
    // ... lots of other stuff
    public BlueJays(int nbPlayers, int nbMonths) {
        this.nbPlayers = nbPlayers;
        this.nbMonths = nbMonths;
        // doesn't matter if you take the parameters or fields
        this.stats = new PlayerStats[nbPlayers][nbMonths];
    }