我正在尝试使用以下语法构建一个数组数组。我的输出绝对不是我想要实现的。这是我的代码:
var labs:ArrayCollection = new ArrayCollection();
var sets:ArrayCollection = new ArrayCollection();
var labsArray:Array = ["ProDPI","WHCC","Tin"];
var setsArray:Array = ["Set1","Set2","Set3"];
var folders:ArrayCollection = new ArrayCollection();
var foldersArray:Array = [labsArray, setsArray];
var objFolderBuild:Object = new Object;
for (var i:int = 0; i< foldersArray.length;i++) {
for (var j:int = 0; j < foldersArray[i].length; j++) {
objFolderBuild.labName = foldersArray[i][j];
folders.addItem(objFolderBuild);
}
}
labFolderList.labelField="labName";
labFolderList.dataProvider=folders;
setFolderList.labelField="setName";
setFolderList.dataProvider=folders;
我的输出是6个引用“Set3”。我的for循环显然有问题,但我无法理解。任何想法都会有很大的帮助。
答案 0 :(得分:2)
我将这个快速应用程序汇总在一起,以证明您的代码正确地描述了foldersArray [i] [j]。它是你应用程序代码的90%。
但是,我想指出objFoldersBuild
对象只创建一次。因此,每次循环执行的操作都会更改现有对象属性的值,并将其添加到文件夹ArrayCollection中。你有一个ArrayCollection与同一个确切对象的多个副本。答案是每次循环创建一个新对象。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" preinitialize="windowedapplication1_preinitializeHandler(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
protected function windowedapplication1_preinitializeHandler(event:FlexEvent):void
{
var labs:ArrayCollection = new ArrayCollection();
var sets:ArrayCollection = new ArrayCollection();
var labsArray:Array = ["ProDPI","WHCC","Tin"];
var setsArray:Array = ["Set1","Set2","Set3"];
var folders:ArrayCollection = new ArrayCollection();
var foldersArray:Array = [labsArray, setsArray];
var objFolderBuild:Object = new Object;
for (var i:int = 0; i< foldersArray.length;i++) {
for (var j:int = 0; j < foldersArray[i].length; j++) {
trace(foldersArray[i][j]);
objFolderBuild = new Object;
objFolderBuild.labName = foldersArray[i][j];
folders.addItem(objFolderBuild);
}
}
/* labFolderList.labelField="labName";
labFolderList.dataProvider=folders;
setFolderList.labelField="setName";
setFolderList.dataProvider=folders; */
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:WindowedApplication>