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.
We'll see how to do this in the next post.
Are there any paralells between ArrayLists and a Pascal Linked List? Seem similar on the surface (array size is not defined at compile time).
Posted by: Joe G. | September 22, 2005 at 12:55 PM
An ArrayList isn't a link listed - different data structure, although both are linear structures. It is funny that for two things that seem so similar, they are used in very different situations...
Link Lists have very fast insert and delete, but linear time lookup. ArrayLists are terrible at inserts (even at the end if you have to resize) but constant time for lookup.
What is also interesting is that there is no Linked List class in .NET 1.x. You have to wait until .NET 2.0 to get it.
Posted by: Brian Tyler | September 22, 2005 at 02:44 PM
One thing to remember though, arryalist being part of the system.collection namespace, it can not be serialized. You can use arraylist.copyto(array) method to copy all the elemements of an arraylist to an array and have all the advantages of an array(fast read, serialization etc).
Posted by: Eugene Geno | February 11, 2007 at 02:09 PM
Actually, that isn't correct. Many classes, including ArrayList, are marked with the serializable attribute. Of course, for the list to be truly serializable, elements you put in must also be serializable.
Posted by: Brian Tyler | February 12, 2007 at 09:35 AM
You are right, i think that using arrays instead of arraylists when possible can improve performance significantly.
Posted by: Thomas | September 14, 2007 at 06:56 PM
sodxcg lqign hzmvse xtehlbgkp jwzdup dqyev blkvfmg
Posted by: hwtjkfigc qapgfx | March 17, 2009 at 03:09 AM
"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."
Wrong, yu simply "Re Dim Preserve" to expand an array.
Further more, if the function that populates an array is a standard NET function (writeallfiles for example) if will auto-size the array for you without you having to state up front what the array size is.
for example (imports system.IO)
Dim array() As String
array = File.ReadAllLines("C:\text.txt")
would make the array the size its needs to be to incorporate all lines
Posted by: Stevo | May 22, 2010 at 07:56 AM
Redim Preserve should be avoided. If a dynamic array is needed, it is better to just use an ArrayList.
And the internal working of the ReadAllLines method might just be using an ArrayList to read the file and do a ToArray() at the end...
Posted by: Wolf | June 03, 2010 at 01:34 AM
The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!
Posted by: new jordans | November 01, 2010 at 01:53 AM
To sensible men, every day is a day of reckoning..
Posted by: cheap air yeezy | November 03, 2010 at 03:02 AM
They say every dog has his day and this dog had his today~
Posted by: Coach Wristlets | January 09, 2011 at 09:31 PM
These particles of light are the fastest things in the universe, so an optical computer could theoretically process information at speeds that make even a supercomputer look glacial.
Posted by: Hobo Handbags | February 25, 2011 at 08:16 PM
Great article and you have indeed covered the topic quite well I have also blogged about Java Arraylist in Java 5 some time back , may be you find interesting.
Posted by: JP@arraylist in java | June 18, 2011 at 08:37 AM
Thanks for your post! The only difference is that ArrayList is scalable. But I think it's worth to notice that when simple array is created an abstract class Array is created and it also implements IEnumerable, ICollection and IList interfaces as it is ArrayList does.
Posted by: Account Deleted | June 26, 2011 at 03:45 AM