使用Arrays.toString()

时间:2018-04-11 21:10:26

标签: arrays

这已被修改为分享我的(几乎完成的)代码。 我正在努力确定我对此缺少什么 line:System.out.println(" is" + Arrays.toString(AvgTemp.getTemp())); 不是打印每一行中的所有元素,而是打印与月份相关的元素。 而不是接收这个: 1月的平均温度为[45.0,40.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

我应该看到这样的事情: 1月份的平均温度为[45.0]

问题是剥离所需元素我做错了什么?

ArrayList<String> monthlist = new ArrayList<String>(12);
    AverageTemperature AvgTemp = new AverageTemperature();

    monthlist.add("January");
    monthlist.add("February");
    monthlist.add("March");
    monthlist.add("April");
    monthlist.add("May");
    monthlist.add("June");
    monthlist.add("July");
    monthlist.add("August");
    monthlist.add("September");
    monthlist.add("October");
    monthlist.add("November");
    monthlist.add("December");

    AvgTemp.Temp[0] = 45;
    AvgTemp.Temp[1] = 40;


    for (int index = 0; index < monthlist.size(); index++)
    {
        for (index = 0; index < monthlist.size(); index++)
        {
        System.out.print("The average temp for ");
        System.out.print(monthlist.get(index));
        System.out.println(" is " + Arrays.toString(AvgTemp.getTemp()));

        /**System.out.println("Temperature at index " + (index + 1) +
                " is: " + Arrays.toString(AvgTemp.getTemp()));
        */
         }
    }

}

}

public class AverageTemperature {
   private double AvgTemp;
   double[] Temp = new double[12];


   public AverageTemperature()
   {
      AvgTemp = 0.0;
   }

   public void setTemp(double[] t)
   {
      Temp = t;
   }

   public double [] getTemp()
   {
      return Temp;
   }
}

1 个答案:

答案 0 :(得分:0)

我不确定AverageTemperature课程的重点是什么。它似乎只是一个数组的包装器。我假设这是一项正在进行中的工作而忽略了这一点。

getTemp()返回所有月份的数组&#39;临时工。您需要索引该数组以打印单个值:

for (int index = 0; index < monthlist.size(); index++)
{
    // Why are there 2 for loops?
    //for (index = 0; index < monthlist.size(); index++)
    //{
    System.out.print("The average temp for ");
    System.out.print(monthlist.get(index));
    // Don't use toString, just index the array
    System.out.println(" is " + AvgTemp.getTemp()[index]);
    //}
}

或者:

double[] avgTemps = AvgTemp.getTemp();
for (int index = 0; index < monthlist.size(); index++)
{
    System.out.print("The average temp for ");
    System.out.print(monthlist.get(index));
    System.out.println(" is " + avgTemps[index]);
}

对我来说,让getTemp()获取索引并返回单个值更合乎逻辑。与setTemp()相同:

public class AverageTemperature {
   private double AvgTemp;
   // Make this private too
   private double[] Temp = new double[12];

   public AverageTemperature()
   {
      AvgTemp = 0.0;
   }

   public void setTemp(int index, double value)
   {
      // TODO: Add some index bounds checking
      Temp[index] = value;
   }

   public double getTemp(int index)
   {
      // TODO: Add some index bounds checking
      return Temp[index];
   }
}

然后,将您的代码更改为:

AvgTemp.setTemp(0, 45);
AvgTemp.setTemp(1, 40);

for (int index = 0; index < monthlist.size(); index++)
{
    System.out.print("The average temp for ");
    System.out.print(monthlist.get(index));
    System.out.println(" is " + AvgTemp.getTemp(index));
}

<子> 您可能还想查看可用于提高可读性的Month Enum