-1

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
shashu10
  • 303
  • 1
  • 6
  • 14
  • This is a very similar question http://stackoverflow.com/questions/412813/when-to-use-arraylist-over-array-in-c – Ian G Jul 22 '11 at 18:11

2 Answers2

3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

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.

Patrick87
  • 27,682
  • 3
  • 38
  • 73