Monday 15 April 2013

Java Average of MultiDimensional List -



Java Average of MultiDimensional List -

i having problem calculating average multidimensional list. read of questions , answers here , other places not find solution. hope guys can me help out! new java, please bare me.

my codes generate list values shown below (4 coulums). trying calculate average each column don't know how to. since list not have length property, tried convert whole thing array can determine length, sum , take average. info place in hashmap (map) , dump list (maplist). please show me how this? appreciate inputs. give thanks you!

array linkedlist arraylist vector [44, 40, 80, 81] [43, 56, 61, 42] [47, 34, 64, 41] [37, 39, 86, 66] [31, 30, 55, 43]

here codes.

private void setmap(){ // print headers system.out.printf("%5s%12s%10s%8s\n","array", "linkedlist", "arraylist", "vector"); (int = 0; < numbertimes; i++){ // set calculated time hashmap map.put("array", timer.arraytimes()); map.put("linkedlist", timer.linkedtimes()); map.put("arraylist", timer.listtimes()); map.put("vector", timer.vectortimes()); // set hashmap values list maplist = new arraylist(map.values()); // iterate through maplist list iterator iter = maplist.iterator(); // print hashmap values while (iter.hasnext()){ // print values in numbers system.out.printf("%-10d", iter.next()); } // end while system.out.println(); } // end } // end setmap

you can utilize enhanced for-loop loop through each element in hash map, following:

private void setmap(){ // print headers system.out.printf("%5s%12s%10s%8s\n","array", "linkedlist", "arraylist", "vector"); (int = 0; < numbertimes; i++){ // set calculated time hashmap map.put("array", timer.arraytimes()); map.put("linkedlist", timer.linkedtimes()); map.put("arraylist", timer.listtimes()); map.put("vector", timer.vectortimes()); // loops through each element of map.values() , assigns value val variable for(string val : map.values()){ system.out.printf("%-10d", val); } system.out.println(); } // end }

please not need alter "string" in loop info type held hashmap. can size of hashmap using .size() method, example:

hashmap<string, integer> map = new hashmap<string, integer>(); map.size();

java list arraylist average

No comments:

Post a Comment