[英]Java Average of MultiDimensional List
我在為多維列表計算平均值時遇到問題。 我在這里和其他地方也閱讀了大多數問題和答案,但是找不到解決方案。
我的代碼生成一個列表,其值如下所示(4列)。 我試圖計算每列的平均值,但我不知道該怎么做。 由於列表不具有length屬性,因此我嘗試將整個對象轉換為數組,以便確定長度,然后求和取平均值。 數據放置在哈希圖(地圖)中,然后轉儲到列表(mapList)中。 請告訴我該怎么做? 真的感謝任何投入。 謝謝!
Array LinkedList ArrayList Vector
[44, 40, 80, 81]
[43, 56, 61, 42]
[47, 34, 64, 41]
[37, 39, 86, 66]
[31, 30, 55, 43]
這是代碼。
private void setMap(){
// print headers
System.out.printf("%5s%12s%10s%8s\n","Array", "LinkedList",
"ArrayList", "Vector");
for (int i = 0; i < numberTimes; i++){
// put calculated time into hashmap
map.put("Array", timer.arrayTimes());
map.put("LinkedList", timer.linkedTimes());
map.put("ArrayList", timer.listTimes());
map.put("Vector", timer.vectorTimes());
// put hashmap values into 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 for
} // end setMap
由於列表不具有length屬性,因此我嘗試將整個對象轉換為數組,以便確定長度,然后求和取平均值。
列表不具有length屬性,但是具有size屬性。 例如,
List<T> list = new ArrayList<T>();
list.add((T) object1);
list.add((T) object2);
System.out.println(list.size());
希望這將為您設置獲取列表大小的路徑。
因此,如果您將列表放入地圖,則針對地圖的每個項目在地圖上進行迭代,對大小進行迭代,對其進行計數並獲得平均值。
您使您的代碼非常混亂並且出現了復雜性,因此下面是我使用Hashmap技術求平均值的代碼。下面是我的代碼,請理解這一點並再次嘗試實現..謝謝
import java.util.*;
public class QueQue {
public static float getAverage(HashMap<String, ArrayList<Integer>> hm, String name) {
ArrayList<Integer> scores;
scores = hm.get(name);
if (scores == null) {
System.out.println("NOT found");
}
int sum = 0;
for (int x : scores) {
sum += x;
}
return (float) sum / scores.size();
}
//Find the Best Student from these Key-Value Pair
public static String getBestStudent(HashMap<String, ArrayList<Integer>> hm) {
double max = 0;
String beststudent = null;
for (Map.Entry<String, ArrayList<Integer>> x : hm.entrySet()) {
String name = x.getKey();
double average = getAverage(hm, name);
if (average > max) {
max = average;
beststudent = name;
}
}
return beststudent;
}
public static void main(String[] args) {
HashMap<String, ArrayList<Integer>> hm = new HashMap<>();
hm.put("Peter", new ArrayList<>());
hm.get("Peter").add(10);
hm.get("Peter").add(10);
hm.get("Peter").add(10);
hm.put("Nancy", new ArrayList<>());
hm.get("Nancy").add(7);
hm.get("Nancy").add(8);
hm.get("Nancy").add(8);
hm.put("Lily", new ArrayList<>());
hm.get("Lily").add(9);
hm.get("Lily").add(9);
hm.get("Lily").add(8);
System.out.println("Find the average of the Peter");
float num = getAverage(hm, "Peter");
System.out.println(num);
String name = getBestStudent(hm);
System.out.println(name);
}
}
您可以使用增強的for循環遍歷哈希圖中的每個元素,如下所示:
private void setMap(){
// print headers
System.out.printf("%5s%12s%10s%8s\n","Array", "LinkedList",
"ArrayList", "Vector");
for (int i = 0; i < numberTimes; i++){
// put calculated time into 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() and assigns its value to the val variable
for(String val : map.values()){
System.out.printf("%-10d", val);
}
System.out.println();
} // end for
}
請注意,您需要在for循環中將“ String”更改為HashMap保留的數據類型。 實際上,您可以使用.size()方法獲取hashMap的大小,例如:
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.size();
如果使用的是ArrayList> stringarray_name = new ArrayList>(),則可以使用輸出數組list.size()來查找總行,然后對每一行進行加法運算並找到平均值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.