gpt4 book ai didi

java - "Incompatible Types"尝试从方法返回数组列表时

转载 作者:行者123 更新时间:2023-12-01 07:07:34 27 4
gpt4 key购买 nike

在这个类中,我想返回整个数组列表,而不是作为单个元素。但是,我在编译时收到错误“不兼容类型”。我在这里做错了什么?感谢您的帮助!!

import java.util.ArrayList;
public class CO2FromElectricity
{



/**
* Constructor for objects of class CO2FromElectricity
*/
public CO2FromElectricity()
{

}

public static double calcAverageBill(ArrayList<Double> monthlyBill, int ind)
{
ArrayList<Double> avgBill = new ArrayList<Double>();
int i = ind;
avgBill.set(i, (monthlyBill.get(i) + avgBill.get(i))/2);
return avgBill.get(i);
}

public double calcAveragePrice(ArrayList<Double> monthlyPrice, int ind)
{
int i = ind;
ArrayList<Double> avgPrice = new ArrayList<Double>();
avgPrice.set(i, (monthlyPrice.get(i) + avgPrice.get(i))/2);
return avgPrice;
}

public double calcElectricityCO2(double avgBill, double avgPrice)
{
double avBill = avgBill;
double avPrice = avgPrice; //Price per kilowatt that is...
double emissions = (avBill/avPrice)*1.37*12;
return emissions;

}
}

最佳答案

calcAveragePrice() 方法中,您返回一个 List,而该方法被定义为返回一个 double

要么将方法签名更改为

public List<Double> calcAveragePrice(...)

或者返回一个double

return list.get(i); //similar to the getAverageBill() method

关于java - "Incompatible Types"尝试从方法返回数组列表时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com