将结果存储在double类型的数组中

时间:2016-01-01 13:25:51

标签: java arrays double

我的计算产生了多个结果。结果是double类型。所以我将结果存储在double []中。

private void calculateProducedEnergy() {
    for (Integer s: mWeatherProfile.getSunshine()) {
        for (Double w: mWeatherProfile.getWeatherConditions()) {
            mProducedEnergy = s * w * mPvSystem.getEfficiency() * mPvSystem.getPanelSurface();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以通过两种方式解决此问题。如果您希望将变量mProducedEnergy保留为数组,那么

private void calculateProducedEnergy() {
 int counter = 0;
 for (Integer s: mWeatherProfile.getSunshine()) {
  for (Double w: mWeatherProfile.getWeatherConditions()) {
   mProducedEnergy[counter++] = s * w * mPvSystem.getEfficiency() * mPvSystem.getPanelSurface();
  }
 }
}

或将mProducedEnergy更改为List<> mProducedEnergy = new ArrayList<>();

private void calculateProducedEnergy() {
 for (Integer s: mWeatherProfile.getSunshine()) {
  for (Double w: mWeatherProfile.getWeatherConditions()) {
   mProducedEnergy.add(s * w * mPvSystem.getEfficiency() * mPvSystem.getPanelSurface());
  }
 }
}