如何在另一个类中给定值填充二维数组

时间:2016-01-15 14:18:08

标签: java arrays multidimensional-array

编辑:没有关于NULL POINTER的事情。不是DUPLICATE - Null指针异常是我得到的所以我知道我没有正确填充数组,这就是我所要求的。我如何填充阵列?

我试图将一个类中的值输入声明为另一个类的2d数组。

它应该工作的方式主要是嵌套循环应该将值提供给另一个方法,然后将其输入到二维数组中。

由于

这是主要的

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","Tulow    itzki","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);

    for(int iPlayer=0; iPlayer<NBPLAYERS; iPlayer++) {
        for(int iMonth=0; iMonth<NBMONTHS; iMonth++) {
         player = new PlayerStats(players[iPlayer], iMonth, battingAvg[iPlayer][iMonth],onBase[iPlayer][iMonth]);

        team.setPlayerStatsCell(player,iPlayer,iMonth);
    }
    }

    team.getHighestSingleMonthBattingAvg();
    team.getHighestOnBase(5);
    team.getLowestBattingAvg(5);
    team.getBestMonth("Bautista");
    team.getBestOverallRecord();
    team.getLowestOnBase();

我尝试使用ATM的方法部分是

PlayerStats setPlayerStatsCell(PlayerStats p, int nbPlayers, int nmMonths)

哪个应该从main获取信息并将其放入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;

  PlayerStats[][] stats;

  public BlueJays(int nbplayers2, int nbmonths2) {
   this.nbPlayers = nbplayers2;
   this.nbMonths = nbmonths2;
   this.stats = new PlayerStats[nbPlayers][nbMonths];
}



PlayerStats setPlayerStatsCell(PlayerStats p, int nbPlayers, int nmMonths) {

for(int i=0; i<nbPlayers; i++) {
    for(int j=0; j<nmMonths; j++) {
        stats[i][j]= p;
    }
}
    return p;
}


PlayerStats getHighestSingleMonthBattingAvg() {

  j = 0;
for(int i = 0; i < nbPlayers; i++)   {
    for(j = 0; j<nbMonths; j++) {
        stats[i][j].getAvg();
        if(highestBattingAvg < stats[i][j].getAvg()) {
           highestBattingMonth = stats[i][j].getMonth();
           highestBattingPlayer = stats[i][j].getName();
        }

        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) {
j = 0;
  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) {
    j = 0;
  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() {
  j = 0;
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() {
   j = 0;
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;
  }
}

这是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;
  }
}

它抛出的异常是一个空指针,数组没有填充。

Exception in thread "main" java.lang.NullPointerException
at BlueJays.getHighestSingleMonthBattingAvg(BlueJays.java:52)
at Lab1.main(Lab1.java:52)
Highest average batting player for the month 0 is null
Highest average batting player for the month 0 is null
Highest average batting player for the month 0 is null
Highest average batting player for the month 0 is null
Highest average batting player for the month 0 is null

1 个答案:

答案 0 :(得分:1)

您的setPlayerStatsCell函数在上下文中没有任何意义。试试这个:

PlayerStats setPlayerStatsCell(PlayerStats p, int nbPlayer, int nmMonth)
{
    stats[nbPlayer][nmMonth] = p;
    return p;
}

错误是由旧的无法设置表的最后一行和列引起的。