Collections-ArrayList - DotNet and DataBase

Thursday, 19 July 2018

Collections-ArrayList





ArrayList:
Arraylist is a class which is derived from System.Collections. Arraylist is to store more than elements with different datatypes.  Array is the next version of the array. Unlike an array, we can change the size of the array dynamically and  we can add remove the elements in the collection.

Syntax:
ArrayList <variablename> = new ArrayList();

Example:
ArrayList al = new ArrayList();

Properties of an Arraylist
Add - To add elements to the arraylist  (al.Add("One");).
Remove - To remove a specific object (al.Remove(5);)
Clear - To delete all the elements from the arraylist (al.Clear();)
RemoveAt - Remove a particular element from the arraylist from a particular index supplied (al.RemoveAt(index)).
RemoveRange - Removes the list of elements from the arraylist based on the index and count supplied (al.RemoveRange(index,range)).
Sort - Sorts the collection in ascending order (al.Sort()).
Reverse - Reverse the elements order in the collection (al.Reverse();).

Example:
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Collections
{
    class ClassArrayList
    {
        public void ArrayListMethod()
        {
            ArrayList al = new ArrayList();
            Console.WriteLine("Adding Elements to the arraylist");
            Console.WriteLine("=================================");
            al.Add(0);
            al.Add(1);
            al.Add(2);
            al.Add(3);
            al.Add(4);
            al.Add("Naga");
            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }

            Console.WriteLine("\n");
            Console.WriteLine("Adding Elements to the arraylist at a particular range");
            Console.WriteLine("=================================");
            int[] a = new int[5] { 5,6,7,8,9};
            al.AddRange(a);
            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }
            Console.WriteLine("\n");
            Console.WriteLine("Removing Elements from the arraylist at a particular index");
            Console.WriteLine("=================================");
            al.RemoveAt(5);

            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }
            Console.WriteLine("\n");
            Console.WriteLine("Removing a particular element from the arraylist");
            Console.WriteLine("=================================");
            al.Remove(5);
            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }

            Console.WriteLine("\n");
            Console.WriteLine("Removing a list elements from the arraylist which are in a particular range");
            Console.WriteLine("=================================");
            al.RemoveRange(7, 2);
            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }

            
            Console.WriteLine("\n");
            Console.WriteLine("Removing all elements from the arraylist");
            Console.WriteLine("=================================");
            al.Clear();
            foreach (var item in al)
            {
                Console.WriteLine("Array list item value is : " + item);
            }
        }
        static void Main()
        {
            ClassArrayList c = new ClassArrayList();
            c.ArrayListMethod();
            Console.Read();

        }        
    }
}



Output
Adding Elements to the arraylist
=================================
Array list item value is : 0
Array list item value is : 1
Array list item value is : 2
Array list item value is : 3
Array list item value is : 4
Array list item value is : Naga


Adding Elements to the arraylist at a particular range
=================================
Array list item value is : 0
Array list item value is : 1
Array list item value is : 2
Array list item value is : 3
Array list item value is : 4
Array list item value is : Naga
Array list item value is : 5
Array list item value is : 6
Array list item value is : 7
Array list item value is : 8
Array list item value is : 9


Removing Elements from the arraylist at a particular index
=================================
Array list item value is : 0
Array list item value is : 1
Array list item value is : 2
Array list item value is : 3
Array list item value is : 4
Array list item value is : 5
Array list item value is : 6
Array list item value is : 7
Array list item value is : 8
Array list item value is : 9


Removing a particular element from the arraylist
=================================
Array list item value is : 0
Array list item value is : 1
Array list item value is : 2
Array list item value is : 3
Array list item value is : 4
Array list item value is : 6
Array list item value is : 7
Array list item value is : 8
Array list item value is : 9


Removing a list elements from the arraylist which are in a particular range
=================================
Array list item value is : 0
Array list item value is : 1
Array list item value is : 2
Array list item value is : 3
Array list item value is : 4
Array list item value is : 6
Array list item value is : 7


Removing all elements from the arraylist
=================================



Practice

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.