feat: modify some Dart codes and add Dart code blocks to the docs (#543)

This commit is contained in:
liuyuxin
2023-06-02 14:56:29 +08:00
committed by GitHub
parent 53e18bc6d6
commit 281c0c618a
25 changed files with 339 additions and 54 deletions

View File

@@ -226,7 +226,24 @@
=== "Dart"
```dart title="hash_map.dart"
/* 初始化哈希表 */
Map<int, String> map = {};
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map[12836] = "小哈";
map[15937] = "小啰";
map[16750] = "小算";
map[13276] = "小法";
map[10583] = "小鸭";
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map[15937];
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10583);
```
遍历哈希表有三种方式,即 **遍历键值对、遍历键、遍历值**。
@@ -390,7 +407,21 @@
=== "Dart"
```dart title="hash_map.dart"
/* 遍历哈希表 */
// 遍历键值对 Key->Value
map.forEach((key, value) {
print('$key -> $value');
});
// 单独遍历键 Key
map.keys.forEach((key) {
print(key);
});
// 单独遍历值 Value
map.values.forEach((value) {
print(value);
});
```
## 哈希函数