Arrays And ArrayList Quick Look

Arrays

Arrays are strongly-typed collection of same datatype and have fixed length, which cannot be changed during runtime. Actually, an array stores values with an index, basis which, it will start with zero. If we want to access values from the arrays, we need to pass the index values.
 
Arrays Declaration

Generally, we will declare the arrays with the fixed length and store values, as shown below-
  1. string[] ar=new string[3];  
  2.   
  3. ar[0] = ”Asp”;  
  4.   
  5. ar[1] = “Java”;  
  6.   
  7. ar[2] = “PHP”;  
In the code, shown above, I declared an array size 3, which means we can store only 3 string values in an array.
 
Arraylists
ArrayList does not have type restriction to store the data, which means it is not Type Safe. You can store anything in an ArrayList. In fact, same ArrayList can store the multiple types of objects. Arraylist size will depend on the values of its arrays, which means Arraylists size will increase or decrease dynamically. It can store any size of values form any datatypes. ArrayList belongs to the System.Collections namespace.

Example:
  1. //For Adding string Values in List  
  2.   
  3. List<string> strlist = new List<string>(); 
  4.   
  5. strlist.Add("Cat");  
  6.   
  7. strlist.Add("Ball");  
  8.   
  9. strlist.Add("Apple");  
  10.   
  11. strlist.Add("Dog");  
  12.   
  13. For Adding integer Values in List  
  14.   
  15. List<int> intList = new List<int>();  
  16.   
  17. intList.Add(1);  
  18.   
  19. intList.Add(5);  
  20.   
  21. intList.Add(4);  
  22.   
  23. intList.Add(2);  
  24.   
  25. intList.Add(3);  
ArrayList Declaration
  1. ArrayList arrList = new ArrayList();  
  2.   
  3. ArrayList Forintsort = new ArrayList();  
  4.   
  5. ArrayList Forstringsort = new ArrayList();  
Adding Values In ArrayList
  1. arrList.Add(strlist); // String List Type Values  
  2.   
  3. arrList.Add(intList); // Integer List Type Values  
  4.   
  5. arrList.Add(251); // Integer type values  
  6.   
  7. arrList.Add(1.0002); // Float type values  
  8.   
  9. arrList.Add(DateTime.Now);// For DateTime type  
  10.   
  11. arrList.Add(“This is Arraylist”); // String type values  
In the code, shown above,I have not mentioned any size in array list and an Arralist can add any datatype values and no size limit is there. Note: While running a loop on ArrayList, you need to use Object data type. Thus, this is another disadvantage as you again do not know, what type of particular data item contains.
  1. // For getting Values from arraylist  
  2.   
  3. string str = "Array list values except list are given below" + "<br/>";  
  4.   
  5. foreach (object obj in arrList)  
  6.   
  7. {  
  8.   
  9. if (obj == strlist)// if strlist is exist in arraylist  
  10.   
  11. {  
  12.   
  13. foreach (var strval in strlist)  
  14.   
  15. Forstringsort.Add(strval);  
  16.   
  17. }  
  18.   
  19. else if (obj == intList)// if intlist is exist in arraylist  
  20.   
  21. {  
  22.   
  23. foreach (var intval in intList)  
  24.   
  25. Forintsort.Add(intval);  
  26.   
  27. }  
  28.   
  29. else  
  30.   
  31. str += obj + "</br>";  
  32.   
  33. }  
  34.   
  35. Forstringsort.Sort();// Sort string Arraylist  
  36.   
  37. string strvalues = "The String values with Sorting are given below" + "</br>";  
  38.   
  39. foreach (var strval in Forstringsort)  
  40.   
  41. {  
  42.   
  43. strvalues += strval + "</br>";  
  44.   
  45. }  
  46.   
  47. Forstringsort.Sort();// Sort int Arraylist  
  48.   
  49. string intvalues = "The Integer values with Sorting are given below" + "</br>";  
  50.   
  51. foreach (var intval in Forintsort)  
  52.   
  53. {  
  54.   
  55. intvalues += intval + "</br>";  
  56.   
  57. }  
  58.   
  59.   
  60. Response.Write(strvalues + "<br/>");  
  61.   
  62. Response.Write(intvalues + "<br/>");  
  63.   
  64. Response.Write(str + "<br/>");  
  65.   
  66. }  
Difference Between an Array And ArrayList

 
Arrays
 
ArrayLists
 
These are strongly-typed collection and allow to store the fixed length.
 
Array Lists are not strongly-typed collection and size will increase or decrease dynamically.
 
In arrays, we can store only one datatype either int, string, char etc.
 
In arraylist, we can store all the datatype values.
 
Arrays belong to System.Array namespace.
 
Arraylist belongs to System.Collection namespaces.
Ebook Download
View all
Learn
View all