fork download
  1. import java.util.List;
  2. import java.util.Arrays;
  3.  
  4. public class WeightedAverage
  5. {
  6. public static double mean(List<Integer> numbers, List<Integer> weights)
  7. {
  8. int total = 0;
  9. int totalWeights = 0;
  10. for (int i = 0; i < numbers.size(); i++)
  11. {
  12. total += numbers.get(i) * weights.get(i);
  13. totalWeights += weights.get(i);
  14. }
  15.  
  16. return total / totalWeights;
  17. }
  18.  
  19. public static void main(String[] args)
  20. {
  21. List<Integer> a = Arrays.asList(new Integer[] { 3, 6 });
  22. List<Integer> b = Arrays.asList(new Integer[] { 4, 2 });
  23.  
  24. System.out.println(WeightedAverage.mean(a, b));
  25. }
  26. }
Success #stdin #stdout 0.02s 25648KB
stdin
Standard input is empty
stdout
import java.util.List;
import java.util.Arrays;

public class WeightedAverage
{
    public static double mean(List<Integer> numbers, List<Integer> weights)
    {
        int total = 0;
        int totalWeights = 0;
        for (int i = 0; i < numbers.size(); i++)
        {
            total += numbers.get(i) * weights.get(i);
            totalWeights += weights.get(i);
        }

        return total / totalWeights;
    }

    public static void main(String[] args)
    {
        List<Integer> a = Arrays.asList(new Integer[] { 3, 6 });
        List<Integer> b = Arrays.asList(new Integer[] { 4, 2 });

        System.out.println(WeightedAverage.mean(a, b));
    }
}