gpt4 book ai didi

java - 如何对值求和Java Hashmap

转载 作者:行者123 更新时间:2023-12-04 11:25:41 25 4
gpt4 key购买 nike

<分区>

我需要一些帮助,我试图获得值的总和,但我卡住了我要求和的 map 值

Grades grades = new Grades(Arrays.asList(1,2,3,4));
Grades grades2 = new Grades(Arrays.asList(2,3,4,5));
Grades grades3 = new Grades(Arrays.asList(4,5,6,1));
Grades grades4 = new Grades(Arrays.asList(1,2,2,4));

HashMap<Pupil, Grades> map = new HashMap<Pupil, Grades>();
map.put(pupil, grades);
map.put(pupil1, grades2);
map.put(pupil2, grades3);
map.put(pupil3, grades4);

我试着用for-each来做到这一点

int sum = 0;
for (int a : map.values()) {
sum += a;
}

但是我得到一个错误 “不兼容的类型:Grades cannot be converted to int, line 49”

class Grades{

private List<Integer> grades;

public Grades(List<Integer> grades){
this.grades = grades;
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com