首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以将对象添加到arraycollection内的arraycollection中吗?

可以将对象添加到arraycollection内的arraycollection中吗?
EN

Stack Overflow用户
提问于 2012-04-19 01:14:57
回答 3查看 1.5K关注 0票数 0

我曾尝试将对象添加到ArrayCollection内的ArrayCollection中,但它不起作用。我在下面的实现中得到错误#1009:

代码语言:javascript
复制
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
    identifyArrayCollection[x].speedsArrayCollection.addItem(speedsObj);
}

我可以将speedsObj添加到不在ArrayCollection中的ArrayCollection中。

任何帮助都将不胜感激。

谢谢,

标记

EN

回答 3

Stack Overflow用户

发布于 2012-04-19 01:32:16

下面的代码将项speedObj添加到在名为identifyArrayCollectionArrayCollection的索引x处找到的ArrayCollection中。

代码语言:javascript
复制
identifyArrayCollection.getItemAt(x).addItem(speedsObj);

这就是你要找的吗?

您拥有的代码执行以下操作:

代码语言:javascript
复制
identifyArrayCollection[x] 
//accesses the item stored in identifyArrayCollection 
//with the key of the current value of x
//NOT the item stored at index x

.speedsArrayCollection
//accesses the speedsArrayCollection field of the object
//returned from identifyArrayCollection[x]

.addItem(speedsObj)
//this part is "right", add the item speedsObj to the
//ArrayCollection
票数 0
EN

Stack Overflow用户

发布于 2012-04-19 03:32:53

假设identifyArrayCollection是包含某些对象的ArrayCollection,speedsArrayCollection是定义为identifyArrayCollection中包含的Object类型的变量的ArrayCollection

您应该执行以下操作:

代码语言:javascript
复制
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
    identifyArrayCollection.getItemAt(x).speedsArrayCollection.addItem(speedsObj);
}
票数 0
EN

Stack Overflow用户

发布于 2012-04-19 07:57:27

不要忘记,任何复合对象都需要首先进行初始化。例如(假设初始运行):

有两种方法可以做到这一点:搭载@Sam

代码语言:javascript
复制
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
   if (!identifyArrayCollection[x]) identifyArrayCollection[x] = new ArrayCollection();
   identifyArrayCollection[x].addItem(speedsObj);
}

或者使用匿名对象,如果你真的想使用显式的命名约定-但是要知道,这些都是而不是编译时检查的(也不是使用数组访问器的任何东西):

代码语言:javascript
复制
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
   if (!identifyArrayCollection[x]) 
   {
      var o:Object = {};
          o.speedsArrayCollection = new ArrayCollection();
      identifyArrayCollection[x] = o;
   }
   identifyArrayCollection[x].speedsArrayCollection.addItem(speedsObj);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10214490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档