samedi 16 janvier 2016

How to get multiple averages from ArrayList?

Code below generates 100 sorted random doubles from 0 to 100, then it's calculating average (avg) from those 100 numbers. My intention was to calculate 10 averages from those numbers and then sort them, but in many tries I get or my 100 sorted-random numbers or 1 average :(

How to get those 10 sorted averages?

public class NewMain {

private static Random rand = new Random();


public static void main(String[] args) {

    System.out.println(Arrays.toString(sortowacz(100, 0, 100)));



}

public static Object[] sortowacz(final double length, final double min,
    final double max) {


List<Double> nieposortowanaLista=randList(length,min,max);

Collections.sort(nieposortowanaLista);
return nieposortowanaLista.toArray();

}



private static List<Double> randList(final double length, final double       min,
    final double max) {



List<Double> list=new ArrayList<>();
List<Double> avg = new ArrayList<>();
double sum = 0.0;

for(int i=0;i<length;i++){

    list.add(min + (max-min)*rand.nextDouble());
}

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

        sum += list.get(i);


        }

    avg.add(sum/list.size());




return list;


}
}




Aucun commentaire:

Enregistrer un commentaire