When To Use Array And When To Use An Array List

I have been working with .NET for almost a year now and have slowly begun to gain a deeper understanding but still have a few general questions. I am making more generic classes to handle processing and while doing this I have run into various occasions where I need to use collections to store various string or other class values.

It seems that when using an ArrayList there is a bit of guilt where I am saying "you should try to use just an array". Can anyone comment on this and bring me up to speed. My next project calls for a class that iterates through a collection of another custom classes. I am trying to figure out if I should use an array or an arraylist. My biggest problem with using just an array is I remember an Array having to have a default value when it is created, which stumps me when I am creating a collection in a class to contain other classes and have no clue as to the size of the array.

Any tips and best practices are greatly apperciated.

Thanks agian for all you experts do, you have made this journey very fun and exciting! I am assigning 500 points to share with all those who provide input. Thanks much!
LVL 1
clickclickbangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

strickddCommented:
An array list allows for quick adding of information and does not have a fixed size. That way you use the memory you need when you need it. However, it is slow to access the data.

An array offers quick access, but it is slow at add at a particular index. To do this it needs to move every value from that index to the lenght of the array to the right one space.

If you want to be able to manipulate the information faster by adding at particular spots in the object, us an arraylist. If you want to be able to access quickly, use an array.

Hope this helps. If you need more information just let me know.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrichmonCommented:
Well that is an interesting question.

An arraylist in either version has an advantage over arrays if you cannot determine the size in advance or if the size will change frequently (with adding and removing elements)

If you KNOW the size in advance - or can determine at the time of creation - then an array is better.  Also an array will be of a specific data type, whereas ArrayList is of type object so you have to cast back out anything that goes in.

Now in .NET 2 this has changed since there is no more arraylist, but instead List<type> which basically is a strongly typed arrayList.  This gets rid of one of the main disadvantages of an arraylist over arrays.


Hope those comments help.
devsolnsCommented:
If 1.1 it perhaps depends on what types your workng with.  If your working with primitive types then you might want to implement your own ArrayList.  ArrayList take objects which would require boxing on all your primitive types. (If not a big list then who cares).  Also an ArrayList is not type safe at all.  You can fill it will anything and mistakenly cast whats coming out of it.  So if you use an ArrayList in 1.1 WITHOUT QUESTION you should create a type safe wrapper around it.

If 2.0 as mentioned above Generics takes care of all the the 1.1 concerns.


If you super performance is your main concern (and you knwo what your doing) then stick with arrays and turn off the array bounds check that occurs each time you call an array like,

class App {
   unsafe static void Main() {

      // Construct an array consisting of 5 Int32 elements
      Int32[] arr = new Int32[] { 1, 2, 3, 4, 5 };

      // Obtain a pointer to the array's 0th element
      fixed (Int32* element = &arr[0]) {

         // Iterate through each element in the array
         // NOTE: The code below has a bug!
         for (Int32 x = 0, l = arr.Length; x <= l; x++) {
            Console.WriteLine(element[x]);
         }
      }
   }
 

-gp
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

clickclickbangAuthor Commented:
mrichmon - Can you give me a link or a quick example of the strongly typed array list?

Thanks much to all for all your input...
clickclickbangAuthor Commented:
This would be the 2.0 version of the List<type>
mrichmonCommented:
List<int> myIntList;

THat would then be the equivalent of a listarray of type int.  Youcan also use any custom class type you have created.

Thaway when you access the items in the list they are already typed to ints or your class and it will throw an error if you did:

myIntList.Add("hello");

since that is a string.  WHereas in 1.1 that would have worked since all items are objects.

http://msdn2.microsoft.com/en-us/library/6sh2ey19(VS.80).aspx
clickclickbangAuthor Commented:
Does the List<Type> only work for primiative types or can you use custom objects?
mrichmonCommented:
Custom types work too.
Kyle AbrahamsSenior .Net DeveloperCommented:
The generics are essentially templates from C++.  As long as the operators are defined, the template will work as well.  
devsolnsCommented:
but oh sooo much better than templates and java generics for that matter.
clickclickbangAuthor Commented:
Thanks much guys. I have already begun using the ListType in my applications! EE, always a pleasure!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.