Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;“不兼容类型”;尝试从方法返回arraylist时_Java_Arraylist - Fatal编程技术网

Java &引用;“不兼容类型”;尝试从方法返回arraylist时

Java &引用;“不兼容类型”;尝试从方法返回arraylist时,java,arraylist,Java,Arraylist,在这个类中,我希望返回一个完整的arraylist,而不是作为单个元素。但是,我在编译时遇到了一个错误,“不兼容类型”。我做错了什么?谢谢你的帮助 import java.util.ArrayList; public class CO2FromElectricity { /** * Constructor for objects of class CO2FromElectricity */ public CO2FromElectricity() { } public static

在这个类中,我希望返回一个完整的arraylist,而不是作为单个元素。但是,我在编译时遇到了一个错误,“不兼容类型”。我做错了什么?谢谢你的帮助

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;

}
}
import java.util.ArrayList;
公共级二氧化碳微电
{
/**
*CO2Frometric类对象的构造函数
*/
公共二氧化碳微电()
{
}
公共静态双重平均账单(ArrayList monthlyBill,int ind)
{
ArrayList avgBill=新的ArrayList();
int i=ind;
avgBill.set(i,(monthlyBill.get(i)+avgBill.get(i))/2);
返回avgBill.get(i);
}
公共双平均价格(ArrayList monthlyPrice,int ind)
{
int i=ind;
ArrayList avgPrice=新的ArrayList();
avgPrice.set(i,(monthlyPrice.get(i)+avgPrice.get(i))/2);
返回平均价格;
}
公共双CalCelectricyCO2(双avgBill,双avgPrice)
{
双重平均票据=平均票据;
double avPrice=avgPrice;//每千瓦的价格,即。。。
双重排放=(平均账单/平均价格)*1.37*12;
返回排放;
}
}

calAveragePrice()
方法中,您将返回一个
列表
,而该方法定义为返回一个
双精度

将方法签名更改为

public List<Double> calcAveragePrice(...)
public ArrayList平均价格(ArrayList monthlyPrice,int ind){
int i=ind;
ArrayList avgPrice=新的ArrayList();
avgPrice.set(i,(monthlyPrice.get(i)+avgPrice.get(i))/2);
返回平均价格;
}

您的calcAveragePrice正在返回一个列表(正如其他人已经提到的)

但这也没有任何意义。您创建了大小为0的列表,但尝试在列表中设置一个不存在的索引(因为列表为空),然后返回它。此函数即使已修复,以便编译,也将始终抛出IndexOutOfBoundsException,除非ind为0

很可能您根本不想要列表,如果您确实想要列表,则需要正确使用它。

public double calcAveragePrice(ArrayList monthlyPrice,int ind)
public double calcAveragePrice(ArrayList<Double> monthlyPrice, int ind)
该方法期望返回一个double

ArrayList<Double> avgPriAce = new ArrayList<Double>();
...
return avgPrice;
ArrayList avgPriAce=new ArrayList();
...
返回平均价格;
您正在返回一个arrayList。将代码更改为如下所示:

public ArrayList<Double> calcAveragePrice(ArrayList<Double> monthlyPrice, int ind)
public ArrayList平均价格(ArrayList monthlyPrice,int ind)
这应该可以解决问题


再想一想,你的方法真的很奇怪。我肯定你不想把arraylist作为一个平均值。你肯定想要平均两倍


如果我的假设是正确的,你是在尝试将每个月的价格相加,然后返回平均值?在这种情况下,需要循环数组中的每个值,将它们相加,除以元素数,得到平均值,并将其作为双精度返回。只有我的2美分。

以后,发布堆栈跟踪。另外,堆栈跟踪最肯定地是给你行号,这显然是返回错误的类型。老实说,我从来没有听说过“堆栈跟踪”-计算机课不教的东西,哈哈。谢谢!我的错误。在本例中,您将包括编译器错误。堆栈跟踪是一个运行时错误。当你看到它时,你就会知道它。:)仍将繁荣。运行时索引超出范围。@TimB您在说什么?我不打算为OP编写代码。我只是指出了问题所在。您发布的代码仍然无效,它只解决了一半的问题。不过你现在已经修好了。(不是我否决了你,所以我无法删除否决票)不一定会失败,因为它检查范围
(index<0 | | index>=size())
(重要位小于或等于
size()
)。当然,如果您没有将正确的索引值传递给该方法,那么这仍然是在玩火。对于所有索引情况,它都会引发IndexOutOfBoundsException,因为列表的大小始终为0,请阅读代码。阅读文档。如果列表为空,则其大小为
0
。如果OP碰巧将
0
作为
ind
的值传递给
i
,则
list.set(i,…)
不会失败,而是将元素插入列表中。我并不是在为OP使用的逻辑辩护,它很容易失败,但也不能保证一定会失败。
public ArrayList<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 calcAveragePrice(ArrayList<Double> monthlyPrice, int ind)
ArrayList<Double> avgPriAce = new ArrayList<Double>();
...
return avgPrice;
public ArrayList<Double> calcAveragePrice(ArrayList<Double> monthlyPrice, int ind)