Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList versus an array of objects versus Collection of T

I have a class Customer (with typical customer properties) and I need to pass around, and databind, a "chunk" of Customer instances. Currently I'm using an array of Customer, but I've also used Collection of T (and List of T before I knew about Collection of T). I'd like the thinnest way to pass this chunk around using C# and .NET 3.5.

Currently, the array of Customer is working just fine for me. It data binds well and seems to be as lightweight as it gets. I don't need the stuff List of T offers and Collection of T still seems like overkill. The array does require that I know ahead of time how many Customers I'm adding to the chunk, but I always know that in advance (given rows in a page, for example).

Am I missing something fundamental or is the array of Customer OK? Is there a tradeoff I'm missing?

Also, I'm assuming that Collection of T makes the old loosely-typed ArrayList obsolete. Am I right there?

like image 523
rp. Avatar asked Feb 21 '09 20:02

rp.


People also ask

What is the difference between ArrayList and array of objects?

Array is a fixed length data structure whereas ArrayList is a variable length Collection class. We cannot change length of array once created in Java but ArrayList can be changed. We cannot store primitives in ArrayList, it can only store objects. But array can contain both primitives and objects in Java.

What is the difference between collections and ArrayList?

The Collections API is a set of classes and interfaces that support operations on collections of objects. Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap. Example of interfaces: Collection, Set, List and Map. Whereas, ArrayList: It is re-sizable array implementation.

When should you use an ArrayList vs an array?

Since an array is static in nature i.e. you cannot change the size of an array once created, So, if you need an array which can resize itself then you should use the ArrayList. This is the fundamental difference between an array and an ArrayList.

Is ArrayList more efficient than array?

Arrays are better in performance. ArrayList provides additional functionality such as "remove" at the cost of performance. because it's backed by an underlying array. therefore, anything wrapping an array cannot be faster than the array.


1 Answers

Yes, Collection<T> (or List<T> more commonly) makes ArrayList pretty much obsolete. In particular, I believe ArrayList isn't even supported in Silverlight 2.

Arrays are okay in some cases, but should be considered somewhat harmful - they have various disadvantages. (They're at the heart of the implementation of most collections, of course...) I'd go into more details, but Eric Lippert does it so much better than I ever could in the article referenced by the link. I would summarise it here, but that's quite hard to do. It really is worth just reading the whole post.

like image 56
Jon Skeet Avatar answered Sep 29 '22 11:09

Jon Skeet