none
Array vs ArrayList

    Question

  • Please explain me the difference.

    I was asked about the

    1) normal difference between these

    2) and Array vs ArrayList performance issues as well.

    Monday, June 25, 2007 6:18 PM

Answers

  • An arraylist is dynamically sized whereas an array is not.  You can iterate over an array list using a foreach and you can call the Add() method on an arraylist.

     

    Performance wise an array is typically better for performance.  With an array list it is similar internally to an object[] which requires a cast to get it to the correct object.  With .NET 2.0 and generics you can avoid these casts by using some of the generic collections or creating your own from IList<T>, etc.

    Monday, June 25, 2007 6:52 PM
  • The answer to your question and information on other common structures can be found here. http://msdn2.microsoft.com/en-us/library/aa289148(VS.71).aspx  There is also a 2.0 version which uses the List<>.  Below are two exerpts from the article to help answer your question:

     

     

    Arrays are one of the simplest and most widely used data structures in computer programs. Arrays in any programming language all share a few common properties:

    • The contents of an array are stored in contiguous memory.
    • All of the elements of an array must be of the same type; hence arrays are referred to as homogeneous data structures.
    • Array elements can be directly accessed.

    Behind the scenes the ArrayList uses a System.Array of type object. Since all types are derived either directly or indirectly from object, an object array can hold elements of any type. By default, an ArrayList creates a 16-element object array, although the precise size can be specified through a parameter in the constructor or the Capacity property. When adding an element thought the Add() method, the number of elements in the internal array is checked with the array's capacity. If adding the new element causes the count to exceed the capacity, the capacity is automatically doubled and the array is redimensioned.

     

    Tuesday, June 26, 2007 1:57 AM

All replies

  • An arraylist is dynamically sized whereas an array is not.  You can iterate over an array list using a foreach and you can call the Add() method on an arraylist.

     

    Performance wise an array is typically better for performance.  With an array list it is similar internally to an object[] which requires a cast to get it to the correct object.  With .NET 2.0 and generics you can avoid these casts by using some of the generic collections or creating your own from IList<T>, etc.

    Monday, June 25, 2007 6:52 PM
  • The answer to your question and information on other common structures can be found here. http://msdn2.microsoft.com/en-us/library/aa289148(VS.71).aspx  There is also a 2.0 version which uses the List<>.  Below are two exerpts from the article to help answer your question:

     

     

    Arrays are one of the simplest and most widely used data structures in computer programs. Arrays in any programming language all share a few common properties:

    • The contents of an array are stored in contiguous memory.
    • All of the elements of an array must be of the same type; hence arrays are referred to as homogeneous data structures.
    • Array elements can be directly accessed.

    Behind the scenes the ArrayList uses a System.Array of type object. Since all types are derived either directly or indirectly from object, an object array can hold elements of any type. By default, an ArrayList creates a 16-element object array, although the precise size can be specified through a parameter in the constructor or the Capacity property. When adding an element thought the Add() method, the number of elements in the internal array is checked with the array's capacity. If adding the new element causes the count to exceed the capacity, the capacity is automatically doubled and the array is redimensioned.

     

    Tuesday, June 26, 2007 1:57 AM
  • Thanks a lot.

    Regards,

    Tuesday, June 26, 2007 6:46 PM