Do I use Array or Arraycollection?

Many developers first getting to Flex quickly find out about the ArrayCollection Class and fall in love.

“Really? It’s like an Array PLUS it has binding and filtering and all sorts of other things? Goodbye Array!”

Next thing you know the array is nothing to the developer except for a property that can be accessed via the ArrayCollection().source property. But there are definite times that you need to make the distinct decision between using an Array and an ArrayCollection.

I think it is easier to look at the cases when you would use an ArrayCollection than to look at the cases when you would not use an ArrayCollection. In my opinion, developers should only use ArrayCollections when the Array will be bound to a view. That’s it, simply enough. Binding an Array to a view? Send it through an ArrayCollection so that if the Array updates or changes the CollectionChange event updates the view.

So when would you use an Array? The rest of the time. There is nothing built into an ArrayCollection that isn’t already part of the Array. Plus, you don’t have to deal with the overhead of the ArrayCollection wrapper if it is unnecessary.

I have seen some applications where I have sorted a DataGrid (bound to an ArrayCollection) and another List has updated unexpectedly. This is because the sorting information is shared between Collections, while an Array’s sorting information is not. To solve this unexpected binding I have even seen some developers use the

ObjectUtil.copy()

facility. THIS IS A BAD DECISION. This is bad both for memory and speed depending on how often you are making this copy. Instead, pass around a regular Array and set a

new ArrayCollection(array)

to your view components.

I am open and would love to see you respond back if you disagree or have another specific example of when to use an ArrayCollection vs an Array.

Comments (12)

KlrSpzApril 3rd, 2009 at 10:06 pm

The only time I’ve ever explicitly used an ArrayCollection is when providing a dataProvider… Generally I use Array, and it suffices for everything I’ve needed so far.

Jonathan CamposApril 3rd, 2009 at 11:37 pm

I agree that array will work out quite often, but when you link to an ilistvollection view you should use an arraycollection such as with a list component or datagrid.

wjackerOctober 28th, 2009 at 5:04 am

Good post, Really help me understand the difference between the array and arraycollection. Thanks Jonathan.

anonymenetNovember 23rd, 2009 at 4:22 pm

Hoo, wasn’t expecting that sorting feature of ArrayCollection was shared among components using it ! Your solution is great and don’t forget to bind that array of data !

[Bindable]
var array:Array;

Jonathan CamposNovember 23rd, 2009 at 4:24 pm

Yup, just remember that if you set a dataProvider for a List based component, the array will be turned to ArrayCollection, and XML/XMLList or XMLListCollection.

Jeffry HouserJuly 19th, 2010 at 2:42 pm

I just came across this when you linked to it from StackOverflow. I have a few thoughts I’d thought I’d share.

You said:
“[A collection is] like an Array PLUS it has binding ”

There is no reason why Array’s can’t be used as the source for data binding. See the dayNames property of the DateChooser as one example.

You said:
Collections do have a collectionChange event, which list based classes will listen for and update display based on those changes. But, that isn’t the same as binding.

“There is nothing built into an ArrayCollection that isn’t already part of the Array.”

This is incomplete at best, wrong at worse. The sorting and filtering functionality of a Collection class are things you do not get with a simple Array. I have used the sorting functionality independent of any view components. The dayRenderers of the Flextras Calendar are sorted in order of their date before the collection is looped over and days are placed on the display.

You also say “sorting information is shared between Collections”.

This is a bit misleading. The example you use is that a collection is shared between two views. It is true that if you sort the collection the changes will be reflected in both views. But, sorting is never shared between two separate collections.

Although, I do agree that Collections are best suited for use in views.

Jonathan CamposJuly 19th, 2010 at 3:04 pm

@Jeffry, I completely agree that for many of these points I oversimplified the answer.

For the quote at the top, it was meant to be an exaggeration as to how many early developers see Arrays vs ArrayCollection. Not that it is correct or even close to the entire story.

I agree that Arrays can be used as a point for data binding. The Flex compiler adds many arguments to the original array to be able to listen for changes from the array. Heck, in most components when you pass an Array in, the Array gets wrapped in an ArrayCollection so that the data binding can be used.

I agree also that you get sorting and filtering is in a collection, but I don’t think that you should use an ArrayCollection just to sort an array – yes I’ve seen this practice. Sorting and filtering can be something that is quickly done on the data model with a small function. Obviously the time to worry about these specific points is when you are really getting into performance tuning.

You’re right that I used the wrong words in the last example. The sorting is on the collection, but the two views are tied together using the sorting information. If you were to sort the actual Array source it would not be reflected back up the collection as the collection has it’s own virtual “view” of the data. However, if you were to filter or extract data from the original source Array, that will effect the final collection that is shared between views.

Again, sorry for an over simplification but I’m glad to see people pushing back and wanting to make sure the correct information is being shown.

Jeffry HouserJuly 19th, 2010 at 7:05 pm

“I agree also that you get sorting and filtering is in a collection, but I don’t think that you should use an ArrayCollection just to sort an array”

What would you do instead? Write your own quicksort algorithm?

Jonathan CamposJuly 19th, 2010 at 7:21 pm

Well, I guess the question is how complicated is the sorting? For something really simple you could just use the built in Array.sort() function. I bet that would handle most cases.

For something really complicated then yes, I would probably create some sort of utility to change the Array. Unless I am dealing with this being on the view, where we both agree we’d go with an ArrayCollection

Jeffry HouserJuly 20th, 2010 at 6:55 am

Jonathon,

For some reason I thought that Array.sort only worked for simple values, not objects. But reviewing the ASDocs it appears that is not the case. You can easily specify a compareFunction which should handle complex objects.

You’re right, I bet it would handle most cases.

ioanMarch 22nd, 2011 at 4:27 am

Cool! Thx for the article. It really helps… :)

SurajFebruary 24th, 2012 at 7:36 am

Thanks Jonathan,
Your article is very useful.

Leave a comment

Your comment