簡體   English   中英

使用數組作為值創建關聯數組

[英]Create associative array with arrays as values

在 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,示例字符串”。

我將如何從關聯數組內的數組中只打印一個元素。

方括號訪問運算符[ ] 中的參數序列錯誤。 您需要使用正確的符號:

// 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]]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM