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-
- string[] ar=new string[3];
-
- ar[0] = ”Asp”;
-
- ar[1] = “Java”;
-
- 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:
-
-
- List<string> strlist = new List<string>();
-
- strlist.Add("Cat");
-
- strlist.Add("Ball");
-
- strlist.Add("Apple");
-
- strlist.Add("Dog");
-
- For Adding integer Values in List
-
- List<int> intList = new List<int>();
-
- intList.Add(1);
-
- intList.Add(5);
-
- intList.Add(4);
-
- intList.Add(2);
-
- intList.Add(3);
ArrayList Declaration
- ArrayList arrList = new ArrayList();
-
- ArrayList Forintsort = new ArrayList();
-
- ArrayList Forstringsort = new ArrayList();
Adding Values In ArrayList
- arrList.Add(strlist);
-
- arrList.Add(intList);
-
- arrList.Add(251);
-
- arrList.Add(1.0002);
-
- arrList.Add(DateTime.Now);
-
- arrList.Add(“This is Arraylist”);
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.
-
-
- string str = "Array list values except list are given below" + "<br/>";
-
- foreach (object obj in arrList)
-
- {
-
- if (obj == strlist)
-
- {
-
- foreach (var strval in strlist)
-
- Forstringsort.Add(strval);
-
- }
-
- else if (obj == intList)
-
- {
-
- foreach (var intval in intList)
-
- Forintsort.Add(intval);
-
- }
-
- else
-
- str += obj + "</br>";
-
- }
-
- Forstringsort.Sort();
-
- string strvalues = "The String values with Sorting are given below" + "</br>";
-
- foreach (var strval in Forstringsort)
-
- {
-
- strvalues += strval + "</br>";
-
- }
-
- Forstringsort.Sort();
-
- string intvalues = "The Integer values with Sorting are given below" + "</br>";
-
- foreach (var intval in Forintsort)
-
- {
-
- intvalues += intval + "</br>";
-
- }
-
-
- Response.Write(strvalues + "<br/>");
-
- Response.Write(intvalues + "<br/>");
-
- Response.Write(str + "<br/>");
-
- }
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.
|