我在计算多维列表的平均值时遇到问题。我在这里和其他地方阅读了大部分问题和答案,但我找不到解决方案。
我的代码生成一个列表,其值如下所示(4列)。我试图计算每列的平均值,但我不知道如何。由于列表没有length属性,我试图将整个事物转换为数组,以便我可以确定长度,然后总结并取平均值。数据放在散列映射(map)中,然后转储到列表(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
答案 0 :(得分:1)
由于列表没有length属性,我试图转换 整个数组所以我可以确定长度,然后总结 并取平均值。
List没有length属性但是size属性。例如,
List<T> list = new ArrayList<T>();
list.add((T) object1);
list.add((T) object2);
System.out.println(list.size());
希望,这将使您获得获取列表大小的路径。
因此,如果您将列表放在地图中,则迭代地图,对着每个地图项目,迭代大小,计算并获得平均值。
答案 1 :(得分:1)
您做你的代码非常混乱和复杂性出现,所以下面是我寻找使用下面的Hashmap Technique.Refer是我的代码,重新认识这一点,平均码尝试实施..Thanks
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);
}
}
答案 2 :(得分:0)
您可以使用增强的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
}
请注意,您需要更改&#34; String&#34;在for循环中,由HashMap持有的数据类型。 实际上,您可以使用.size()方法获取hashMap的大小,例如:
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.size();
答案 3 :(得分:0)
如果您使用的是ArrayList&gt; stringarray_name = new ArrayList&gt;() 您可以使用ouuter数组list.size()来查找总行,然后处理每行添加值并找到平均值。