我正在做练习,你必须使用java中的arraylists来计算方差。
我认为对于带有双打而不是整数的arraylist可能有单独的计算方法会有所帮助,但它没有。
import java.util.ArrayList;
public class Variance {
// Copy here sum from exercise 63
public static double sum(ArrayList<Integer> list) {
int i = 0;
int sum = 0;
while (i < list.size()) {
sum += list.get(i);
i++;
}
return sum;
}
public static double sumdouble(ArrayList<Double> list) {
int i = 0;
double sum = 0;
while (i < list.size()) {
sum += list.get(i);
i++;
}
return sum;
}
// Copy here average from exercise 64
public static double average(ArrayList<Integer> list) {
double sum = sum(list);
double average = sum / list.size();
return average;
}
public static double averagedouble(ArrayList<Double> lists) {
double sum = sumdouble(lists);
double average = sum / lists.size();
return average;
}
public static double variance(ArrayList<Integer> list) {
double mean = average(list);
int i = list.size() - 1;
ArrayList<Double> varianceList = new ArrayList<Double>();
while (i >= 0) {
//gets value
double value = list.get(i);
value = value - mean;
value = value * value;
varianceList.add(value);
}
double final = averagedouble(varianceList);
return final;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(2);
list.add(7);
list.add(2);
System.out.println("The variance is: " + variance(list));
}
}
netbeans中的错误:不兼容的类型:ArrayList不能 转换为双重
感谢您的帮助。
答案 0 :(得分:0)
您的问题全部在方差方法中。
你需要循环中的减量--i;
并且您必须将返回变量重命名为其他变量,或者直接返回值。您的方法应该是:
public static double variance(ArrayList<Integer> list) {
double mean = average(list);
int i = list.size() - 1;
ArrayList<Double> varianceList = new ArrayList<Double>();
while (i >= 0) {
//gets value
double value = list.get(i);
value = value - mean;
value = value * value;
varianceList.add(value);
--i;
}
return averagedouble(varianceList);
}
此外,我会考虑将大多数while循环切换为for循环,因为它们更适合循环遍历整个数组。
答案 1 :(得分:0)
您错过了递减循环,代码中缺少--i;
。
使用此代码,它已经过优化和清理:
import java.util.ArrayList;
public class Variance
{
ArrayList<Integer> listOfIntegers;
int size;
public Variance(ArrayList<Integer> listOfIntegers)
{
this.listOfIntegers = listOfIntegers;
size = listOfIntegers.size();
}
double getMean()
{
double sum = 0.0;
for(double a : listOfIntegers)
sum += a;
return sum/size;
}
double getVariance()
{
double mean = getMean();
double square = 0;
for(double a :listOfIntegers)
square += (a-mean)*(a-mean);
return square/size;
}
}
答案 2 :(得分:0)
您的方法中有数据类型声明错误:
public static double sum(ArrayList<Integer> list)
这应该返回int
以与int
变量sum
和ArrayList的元素数据类型兼容:
public static int sum(ArrayList<Integer> list)
也在这种方法中:
public static double average(ArrayList<Integer> list) {
//double sum = sum(list);
int sum = sum(list);
double average = sum / list.size();
return average;
}
您应将sum
声明为int
,以便与sum()
方法的返回类型兼容。
variance
方法中的循环是无限的另一个问题,你需要解决它:
while (i >= 0) {
//gets value
double value = list.get(i);
value = value - mean;
value = value * value;
varianceList.add(value);
i--; // decrease i index
}
并且return语句变量不应该是java final
您可以重命名它或只返回method的值:
return averagedouble(varianceList);
答案 3 :(得分:0)
import java.util.ArrayList;
公共类差异{
public static int sum(ArrayList<Integer> list) {
int sum=0;
for(int i:list){
sum+=i;
}
return sum;
}
public static double average(ArrayList<Integer> list) {
double size = list.size();
return(1.0*sum(list)/size);
}
public static double variance(ArrayList<Integer> list) {
// write code here
double asq=0.0;
for(int i:list){
asq = asq + Math.pow((i-average(list)),2);
}
double var = asq/(list.size()) ;
return var;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(2);
list.add(7);
list.add(2);
System.out.println("The variance is: " + variance(list));
}
}