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 Customer
s 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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With