// 定义一个 Map 集合,key 为 int 类型,value 为 string 类型
var map1 as string[int] = {
1 : "一",
2 : "二",
3 : "三"
};
// 通过 2 来访问 map1 中的 二, 输出 二
print(map1[2]);
// 定义一个 Map 集合,key 为 string 类型,value 为 IItemStack 类型
var map2 as IItemStack[string] = {
gold : <minecraft:gold_ingot>,
iron : <minecraft:iron_ingot>,
diamond : <minecraft:diamond>
};
// 通过 iron 来访问 map2 中的 <minecraft:iron_ingot>
print(map2["iron"].displayName);
// 特别的,key 为 string 类型时可以通过点来访问 value
print(map2.iron.displayName);
var map as string[int] = {
1 : "一",
2 : "二",
3 : "三",
};
map[1] = "one"; // 修改
map[2] = "two"; // 修改
map[3] = "three"; // 修改
map[4] = "four"; // 添加
map[5] = "five"; // 添加
var map as string[int] = {
1 : "一",
2 : "二",
3 : "三",
4 : "四"
}
// key 遍历法
for key in map {
print(key); // 输出 1, 2, 3, 4
}
// key-value 遍历法
for key, value in map {
print(key ~ "-->" ~ value); // 输出 1-->一, 2-->二, 3-->三, 4-->四
}
// entry 遍历法
for entry in map.entrySet {
print(entry.key ~ "-->" ~ entry.value); // 输出 1-->一, 2-->二, 3-->三, 4-->四
}
map.keySet // Returns the map's keySet.
map.keys // Returns the map's keySet.
map.values // Returns the map's valueSet.
map.valueSet // Returns the map's valueSet.
map.entrySet // Returns the map's entrySet.
entry.key; // Returns the entry's key.
entry.value; // Returns the entry's value.