求救一个问题
java吧
全部回复
仅看楼主
level 1
public class MapList {
public static void main(String[] args){
List<Map<String,Integer>> list=new ArrayList<>();
Map<String,Integer> map0=new HashMap<>();
map0.put("英镑",8);
map0.put("美元",6);
map0.put("欧元",7);
map0.put("人民币",1);
list.add(map0);
Map<String,Integer> map1=new HashMap<>();
map1.put("人民币",1000);
map1.put("欧元",7000);
map1.put("美元",6000);
map1.put("英镑",8000);
list.add(map1);
Map<String,Integer> resultMap=new HashMap<>();
Set set=null;
Iterator it=null;
for (int i=0; i<list.size(); i++){
set=list.get(i).keySet();
it=set.iterator();
while (it.hasNext()){
resultMap.put(it.next().toString(),0);
}
}
String key="";
int sum=0;
for (int i=0;i<list.size();i++){
set=list.get(i).keySet();
it=set.iterator();
while (it.hasNext()){
key=it.next().toString();
sum+=resultMap.get(key)*list.get(i).get(key);
resultMap.put(key,sum);
}
}
System.out.println(resultMap);
}
}
怎么计算乘法
2023年08月16日 03点08分 1
level 1
得到的结果都是0
2023年08月16日 03点08分 2
level 9
你的第一个for里面,resultMap.put(it.next().toString(),0);这里resultMap.put了0进去,之后第二个循环里sum+=resultMap.get(key)*list.get(i).get(key);resultMap.get出来的都是0,0乘任何数都为0
2023年08月16日 03点08分 3
多谢,实例代码看花眼了,put参数没看见,
2023年08月16日 03点08分
level 8
不如截图
2023年08月16日 09点08分 4
1