Difference Between Array and Arraylist

Array

Arrays are strongly typed collection of same datatype and these arrays are fixed length that can’t be changed during runtime. Arrays we will store values with index basis that will start with zero. If we want to access values from arrays we need to pass index values.

Declaration of Arrays

We can declare arrays as shown below.

  1. using System;  
  2. namespace WebApplication2  
  3. {  
  4.     public partial class _Default: System.Web.UI.Page  
  5.     {  
  6.         protected void Page_Load(object sender, EventArgs e)  
  7.         {  
  8.             // Initialize the array items during declaration  
  9.             string[] strNames = new string[]  
  10.             {  
  11.                 "Ramakrishna",  
  12.                 "Praveenkumar",  
  13.                 "Indraneel",  
  14.                 "Neelohith"  
  15.             };  
  16.             // Declare array and assign values using index  
  17.             string[] strNamesObj = new string[4];  
  18.             strNamesObj[0] = "Ramakrishna";  
  19.             strNamesObj[1] = "Praveenkumar";  
  20.             strNamesObj[2] = "Indraneel";  
  21.             strNamesObj[3] = "Neelohith";  
  22.         }  
  23.     }  
  24. }  

Arraylist

Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace.

Declaration of Arraylist

To know how to declare and store values in array lists check below code.

  1. using System;  
  2. using System.Collections;  
  3. namespace WebApplication2  
  4. {  
  5.     public partial class _Default: System.Web.UI.Page  
  6.     {  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.             ArrayList arrListObj = new ArrayList();  
  10.             // Adding string value to arraylist  
  11.             arrListObj.Add("Ramakrishna Basagalla");  
  12.             // Adding integer value to arraylist  
  13.             arrListObj.Add(1981);  
  14.             // Adding float value to arraylist  
  15.             arrListObj.Add(11.99);  
  16.         }  
  17.     }  
  18. }  

In the above example we have not specified any size to an array list, we can add any number of data there is no size limit.

Difference between Array and ArrayList

Array ArrayList
Arrays are strongly typed collection of same datatype. ArrayList are not strongly typed collection we can store different types of data.
Data is fixed length. We can increase data dynamically.
Arrays belongs to System.Array namespace. ArrayList belongs to System.Collection namespace.
Data can be added using array index. Data can be added using Add method.

Hope this blog will help you in understanding the Array and ArrayList. Happy Coding.

Ebook Download
View all
Learn
View all