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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re asking the wrong question. Whether an
ArrayListmight 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
ArrayListimplementation, 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
ArrayListinstead of a plain array is when you need a class that implements theIListinterface.But keep in mind that the
ArrayListclass should probably be deprecated in later versions of the .NET Framework. TheSystem.Collections.Genericnamespace provides a bunch of generic collection classes that are much more performant and preferable to theArrayList. In this case, you probably want to use theList<T>class, whereTis the class of the objects you want to store in said array.