|
-
Sep 27th, 2010, 08:00 PM
#1
Re: [RESOLVED] Array List of Data Tables
First up, if you're not using .NET 1.x then you shouldn't be using an ArrayList at all. Secondly, you could do it with a collection rather than an array if you do it properly. Remember that collections are specifically designed to grow and shrink dynamically, while arrays are fixed-size. When you create an array it already has all its elements, but they are all empty, like an egg carton. That means that you can set any specific element that you want. With a collection, if you specify a capacity when you create it, that is only a capacity, NOT a size. That is the number of items you can add before the collection must reallocate space internally. The size is still zero to begin with. If the collection has zero items then you can't set an item at a specific index, because there is no such item. You need to call Add on the collection to add a new item. Once an item exists at an index, then you can set it to a new value.
That said, the whole point of collections is to provide array-like behaviour with dynamic resizing. If you know exactly what size your list is then you should be using an array anyway. A collection uses an array internally anyway so using one directly is always going to be more efficient if you don't need the convenience of dynamic resizing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|