gpt4 book ai didi

dart - 在 Dart.lang 中需要一张 map 的 map

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

学习 Dart.lang 。有php背景。在此链接中给出此代码...

Link to full working php algorithm

或将主要部分复制到这里,这是我在 PHP 中得到的并且工作正常。我需要知道如何在 Dart 中做到这一点:

foreach ($floors as $x) { 
$temp = explode("," , $x);
$floor[$temp[0]][$temp[1]] = $temp[2];
}

最佳答案

library map;

void main() {
var temp = """
1,201,70
1,301,71
3,301,31
1,401,79
2,501,2
1,601,171
2,801,61
2,901,100
5,101,54
4,203,23
3,201,112""";

var values = temp.split('\n');
var floors = new Map<int, Map<int, double>>();
values.forEach((f) {
var v = f.trim().split(',');
var fk = int.parse(v[0]);
var rk = int.parse(v[1]);
var rv = double.parse(v[2]);
if(floors[fk] == null) {
floors[fk] = new Map<int,double>();
}
floors[fk][rk]=rv;
});

floors.forEach((fk, fv) {
var tempSum = 0;
var lowest = null;
for(var rk in fv.keys) {
var temp = fv[rk];
tempSum += temp;
if(lowest == null || (lowest > temp)) {
lowest = temp;
}
}
print("Floor #${fk} has ${fv.length} rooms. The average temp is ${tempSum / fv.length}. The lowest temp is ${lowest}.");
});
}

关于dart - 在 Dart.lang 中需要一张 map 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020971/

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