(Sum ArrayList)编写以下方法,返回ArrayList中所有数字的总和:public static double sum(ArrayList <double> list)

时间:2016-03-04 05:48:42

标签: java arraylist

Sum ArrayList编写以下方法,返回ArrayList中所有数字的总和:public static double sum(ArrayList<Double> list)

import java.util.ArrayList;
                import java.util.Scanner;

                public class SumArrayList {
                    public static void main(String[] args) {
                        Scanner in = new Scanner(System.in);

                        ArrayList<Double> list = new ArrayList<>(5);
                        System.out.print("Enter any 5 numbers: ");

                        while (in.hasNextDouble()) {
                            list.add(in.nextDouble());
                            }
                            // print list
                            System.out.println("The list is " + list);  
                        }

                        public static double sum (ArrayList<Double>list, double sum) 
                        {

                            double total = 0.0;
                            sum = total;

                            for(int i = 0; i < list.size(); i++){           
                            sum += list.get(i);         

                            // print sum
                            System.out.println("and " + sum + " is the sum");
                            }

                            return sum;
                        }
                    }

3 个答案:

答案 0 :(得分:2)

你可以轻松地像这样只用一行代码 :(如果你使用Java 8)

public double getSum(List<Double> list) {
    return list.stream().mapToDouble(Double::doubleValue).sum();
}

<强>用法:

List<Double> list = new ArrayList<>();
list.add(2.6);
list.add(2.6);
double count = getSum(list);
System.out.println("count = " + count);

这是输出:

count = 5.2

答案 1 :(得分:0)

我想你问的是如何总结一个数组列表。为此,使用 foreach 循环迭代整个ArrayList。我不确定为什么你需要sum作为参数。最后,您需要在数据类型和名称之间留一个空格。

public static double sum (ArrayList<Double> list) {
   double total = 0.0;
   for (double d : list) total+=d;
   return total;
}

答案 2 :(得分:-4)

  

试试这个......

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class SumArrayList 
{
    public static void main (String[] args)
    {
     Scanner in = new Scanner(System.in);

                    ArrayList<Double> list = new ArrayList<>(5);
                    System.out.print("Enter any 5 numbers: ");

                    while (in.hasNextDouble()) {
                        list.add(in.nextDouble());
                        }
                        // print list
                        System.out.println("The list is " + list);  
                        System.out.println(sum(list));
                    }

                    public static double sum(ArrayList<Double> list) 
                    {

                        double total = 0.0;

                        for(int i = 0; i < list.size(); i++){           
                        total += list.get(i);
                        }
                          // print sum
                        System.out.println("and " + total + " is the sum");
                        return total;
                    }
}