java - How to get multiple averages from ArrayList? -
code below generates 100 sorted random doubles 0 100, it's calculating average (avg) 100 numbers. intention calculate 10 averages numbers , sort them, in many tries or 100 sorted-random numbers or 1 average :(
how 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; (int = 0; < length; i++) { list.add(min + (max - min) * rand.nextdouble()); } (int = 0; < list.size(); i++) { sum += list.get(i); } avg.add(sum / list.size()); return list; } }
this required code calculate 10 averages out of 100 , sort them @ end
public class testaverages { public static void main(string... args) { list<double> averages = sortowacz(1000, 0, 100); averages.foreach(system.out::println); } public static list<double> sortowacz(final double length, final double min, final double max) { list<double> list = new arraylist<>(); random rand = new random(); (int = 0; < length; i++) { list.add(min + (max - min) * rand.nextdouble()); } list<double> averages = new arraylist<>(); while (!list.isempty()) { list<double> sublist = list.sublist(0, (int) (length/10)); double avg = sublist.parallelstream().maptodouble(d -> d).average().getasdouble(); averages.add(avg); list.removeall(sublist); } return averages; } }
Comments
Post a Comment