Mar 30, 2009

Difference between Array and ArrayCollection in ActionScript 3.0

Hi All,

Recently I got a query asking the difference between Array and ArrayCollection in ActionScript 3.0.

Let me draw quick comparison between Array and ArrayCollection:

  1. The elements of the ArrayCollection can be used in bindings that will be continued to be monitored. This is not the case with the normal Array class, once the element from an array is used in a binding, it is no longer monitored.
  2. ArrayCollection provides a rich set of tools for data manipulation


So what does all that mean?


According to the Adobe flex liveDocs

The ArrayCollection class is a wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces. Operations on a ArrayCollection instance modify the data source; for example, if you use the removeItemAt() method on an ArrayCollection, you remove the item from the underlying Array.


Let me take an example to clear this concept.

Say we receive some data through some webservice. Now if we parse the result set into Array, we have only a few tools to manipulate data namely, push() (for adding an element to the end), pop() (for removing the last element), length (the number of indices), etc.


However, the ArrayCollection class provides a suite of immensely convenient "extra" methods that can act on the Array namely the addItemAt(), removeItemAt(). So using these methods, we can directly access any index value of an array.


Apart from that, we can continuously watch the array for any changes at run time. Say, some user operation deletes a value from my ArrayCollection or Add a new item in my ArratyCollection which I am using as an dataProvider for a data grid. In that case, I can see the node getting disappear or visible at the same time.


This can’t be possible in the case of an Array.


This makes filtering of data as simple as eating a cake :)


HTH


Let me know if you have any doubt.


Thanks,

Shaleen Jain

11 comments:

  1. Hi Shaleen,

    Nice blog. Some examples would have improved the article but thanks for taking time to explain the topic so clearly

    ReplyDelete
  2. nice article.Keep it up....

    ReplyDelete
  3. Hi
    Thanks.nice explanation thank u so much

    ReplyDelete
  4. Thanks guys for appreciating the effort..

    ReplyDelete
  5. This simply awesome..
    It cleared off all my doubts..
    Thank you.

    ReplyDelete
  6. Hello, I do not agree with the previous commentator - not so simple

    ReplyDelete
  7. Hi Shaleen Jain...I am too a Flex developer. I failed in creating run time shared libraries in flex. The information in live docs perhaps is not clear for me to create rsl. Can you please give step by step procedure in creating a rsl and accessing it....

    ReplyDelete
  8. That was great, thanks for sharing!

    ReplyDelete