[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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!
0
clickclickbang
Asked:
clickclickbang
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
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.
0
 
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.
0
 
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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...
0
 
clickclickbangAuthor Commented:
This would be the 2.0 version of the List<type>
0
 
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
0
 
clickclickbangAuthor Commented:
Does the List<Type> only work for primiative types or can you use custom objects?
0
 
mrichmonCommented:
Custom types work too.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
The generics are essentially templates from C++.  As long as the operators are defined, the template will work as well.  
0
 
devsolnsCommented:
but oh sooo much better than templates and java generics for that matter.
0
 
clickclickbangAuthor Commented:
Thanks much guys. I have already begun using the ListType in my applications! EE, always a pleasure!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now