i need to store 26 objects in an array. could be less, but not more. Would an arrayList take up more space than an array of 26 objects?
2 Answers
You're asking the wrong question. Whether an ArrayList
might take a bit more space than a plain old array is quite besides the point.
The question that you really need to answer in order to decide which one to use is whether you will ever need to dynamically resize the array.
If the array will only ever contain 26 objects for its entire lifetime, then you should probably stick with a plain old array: myArray[26]
.
But if there's any chance that you need to dynamically resize the array, changing the number of elements that it contains, then you definitely need to go with the ArrayList
implementation, regardless of whether it might take up a bit more space. The flexibility is far more important in this case, vastly outweighing any cost that may be potentially involved.
Another case where you would want to use ArrayList
instead of a plain array is when you need a class that implements the IList
interface.
But keep in mind that the ArrayList
class should probably be deprecated in later versions of the .NET Framework. The System.Collections.Generic
namespace provides a bunch of generic collection classes that are much more performant and preferable to the ArrayList
. In this case, you probably want to use the List<T>
class, where T
is the class of the objects you want to store in said array.

- 239,200
- 50
- 490
- 574
It can't take less... I imagine it can take more, depending on how they do redimensioning. To clarify, if you're storing fewer than 26 items, array lists could be better... If you're storing 26, array lists can't be better and are likely worse.

- 27,682
- 3
- 38
- 73
-
thanks. i think ill stick with arraylist. I think ill have maybe 15 objects on average. – shashu10 Jul 22 '11 at 17:58