Thursday, May 6, 2010

Array vs ArrayList

Array vs ArrayList

I've gotten this question a few times, so I thought it would make a good post. There's some confusion over the concept of an array versus an ArrayList class, and how to convert both into a LabVIEW array. So, let's start with what each actually is.

Arrays

As in other languages, an array is a high performance way of storing a group of data because each element is laid out next to it's neighbor in memory. This allows for very fast access because (a) the code can do a little math and jump quickly to any location in the array, and (b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses).
An array in .NET is actually a class (System.Array), but a special type of class that is well understood by the .NET engine (CLR). Because of this, you can standard array access notation (text languages) such as
foo[3] = 99;

ArrayList

When it comes to an ArrayList, however, you are dealing with a collection. There are several types of collections in .NET (see the System.Collections and System.Collections.Specialized namespaces), but the key thing about them is the interfaces they support (IEnumerable, ICollections, IList, etc). If you look at the definition of these interfaces, you see that collections are all about grouping things together and providing methods to access them.
How do ArrayLists fit into this? Well, an ArrayList is simply a collection class - one that supports the IList interface - whose internal memory structure is an array. The keyword here, however, is internal. The ArrayList is not an array.
When would you choose to use an ArrayList over an array? There are several decisions factors that come into play when choosing how to pick your data structure, but a common reason people use ArrayLists is they don't know how big the array is going to get. If you create an array, you must say up front how large the array is going to be. Once picked, you can't change it.
With an ArrayList, however, if you add one element more than the internal array can handle, the ArrayList automatically creates a larger array and copies the old array into the new array. Again, this is all internal and you don't have to worry about it - other than performance considerations. Creating/destroying/copying arrays isn't something to do without thinking about your design.

LabVIEW

So what does this mean for you, the LabVIEW programmer? Well, the decision of which to use is a topic too large to cover here - it depends heavily on what your application is doing and how you are going to access/update your data (after all, there are dozens of other collections types besides ArrayList). But what if you already have one or the other?
Well, for arrays, no problem. LabVIEW knows all about arrays and will automatically covert a .NET array into a LV array, and vice versa.
But wait, you say, it doesn't in my program! Well, it does. When I've been told that LV isn't converting a user's .NET array into a LV array, it turns out that what they actually have is an ArrayList. Thus this post. [Actually, another time LV can't covert the array is when the reference is to the base System.Array class rather than the classic typed array such as int[10]. In this case, we can't tell at compile time what type of data the array contains and so we are stuck]
The main advantage to an ArrayList collection is that it supports a handy little method called ToArray(). This method creates a copy of the internal array and returns it to you, allowing you to create a LV array. This is much faster than creating a LV array by looping through the ArrayList yourself inside LabVIEW.

No comments:

Post a Comment