使用数组作为值创建关联数组

在cs4中,我试图创建一个关联数组,其中值是数组。这些数组只有两个元素,我想这样调用这两个元素之一:

var array1:Array = [5,"Example String"]
var array2:Array = [7,"Example String 2"]
var associativeArray:Object = {a1:array1,a2:array2}

trace(associativeArray[a1[0]]) // Print out the value of the first element of the first array. Should print out 5

但是,这不会打印出第一个元素。奇怪的是,如果省略“ [0]”,程序会像这样打印整个数组:“ 5,示例字符串”。

我将如何从关联数组内部的数组中仅打印一个元素。

canandasfu123 回答:使用数组作为值创建关联数组

方括号访问运算符 [] 中的参数顺序错误。您需要使用正确的符号:

// The whole collection.
trace(associativeArray);

// The collection element,square bracket notation.
// The key MUST be a String.
trace(associativeArray["a1"]);

// The collection element,dot notation.
trace(associativeArray.a1);

// Access the element of collection element.
trace(associativeArray["a1"][0]);
trace(associativeArray.a1[0]);

// WRONG. Access non-existent element of the collection.
trace(associativeArray[a1[0]]);
trace(associativeArray["a1"[0]]);
本文链接:https://www.f2er.com/2941095.html

大家都在问