💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
Map 是一个键值对组成的容器。一个 Map 也通常被称为一个关联数组、字典或者符号表。下面的代码是要给简短的使用Map的示例: ~~~ class Main { static public function main() { // Maps are initialized like arrays, but // use the map literal syntax with the // ’=>’ operator. Maps can have their // key value types defined explicity var map1:Map<Int, String> = [1 => "one", 2=>"two"]; // Or they can infer their key value types var map2 = [ "one"=>1, "two"=>2, "three"=>3 ]; $type(map2); // Map<String, Int> // Keys must be unique // Error: Duplicate Key //var map3 = [1=>"dog", 1=>"cat"]; // Maps values can be accessed using array // accessors "[]" var map4 = ["M"=>"Monday", "T"=>"Tuesday"]; trace(map4["M"]); //Monday // Maps iterate over their values by // default var valueSum; for (value in map4) { trace(value); // Monday \n Tuesday } // Can iterate over keys by using the // keys() method for (key in map4.keys()) { trace(key); // M \n T } // Like arrays, a new Map can be made using // comprehension var map5 = [ for (key in map4.keys()) key => "FRIDAY!!" ]; // {M => FRIDAY!!, T => FRIDAY!!} trace(map5); } } ~~~ 在后台,Map 是一个抽象类型。在编译时,它被转换为集中特定类型之一,取决于键的类型: * String : `haxe.ds.StringMap` * Int : `haxe.ds.IntMap` * EnumValue : `haxe.ds.EnumValueMap` * {} : `haxe.ds.ObjectMap` Map类型在运行时不存在,被上面的对象之一取代。Map使用它的键类型定义数组访问(第2.8.3节)。 查看Map API 详细了解它的方法。