神刀安全网

C# ArrayList

In this article, we will discuss:

  1. What is an ArrayList?
  2. Constructing an ArrayList
  3. Adding Elements to an ArryList
  4. ArrayList as an Argument
  5. ArrayList AddRange Method
  6. ArrayList Count and Clear Method
  7. ArrayList Sort and Reverse Method
  8. ArrayList Clone Method
  9. ArrayList Contains Method
  10. ArrayList Insert Method
  11. ArrayList InsertRange Method
  12. ArrayList Remove Method
  13. ArrayList RemoveAt Method
  14. ArrayList RemoveRange Method
  15. ArrayList GetRange Method
  16. ArrayList SetRange Method
  17. ArrayList IndexOf Method
  18. ArrayList Synchronized Method
  19. ArrayList BinarySearch Method
  20. Iterating over elements of an ArrayList
  21. Convert Array to ArrayList
  22. Convert ArrayList to Array

1. What is an ArrayList?

  • ArrayList is a resizable implementation of the List interface.
  • This collection dynamically resizes.
  • It uses an array to store its elements.
  • you can add and remove items from a list at a specified position using an index.It also allows dynamic memory allocation, adding, searching and sorting items in the list.

2. ArrayList Properties:

  • Capacity : Gets or sets the number of elements that the ArrayList can contain.
  • Count: Gets the number of elements actually contained in the ArrayList.
  • IsFixedSize: Gets a value indicating whether the ArrayList has a fixed size.
  • IsReadOnly: Gets a value indicating whether the ArrayList is read-only.
  • Item: Gets or sets the element at the specified index.

3. Constructing an ArrayList

ArrayList provides following overloaded constructors:

ArrayList()

Creates empty list with initial capacity

ArrayList(Int32)

Creates empty list with provided initial capacity

ArrayList(ICollection)

Creates list from the elements of the collection provided

Mostly, you would be just using the first constructor to create an arraylist as follows :

ArrayListlist = new ArrayList(); 

4. Adding Elements to an ArryList

This method appends a new element object to the end of the ArrayList. We can keep adding elements to the collection until memory runs out.

C# Program to use ArrayList Add method :

  class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");                     }           } 

5. ArrayList as an Argument :

You can pass ArrayList as an argument in a C# program. Let’s look at below example to see the implementation.

C# Program to use ArrayList as an argument:

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               ArrayListExample(list);           }         static void ArrayListExample(ArrayListlist)         {             foreach (string  i in list)             {                 Console.WriteLine(i);             }         }     } 

Output:

The above C# program will print below output in console:

Csharpstar

Easywcf

TechKatak

ExamIron

6. Return ArrayList

You can return ArrayList from a C# program.Let’s look at below example to understand its implementations.

C# Program to return ArrayList:

 class CollectionExamples     {         static void Main(string[] args)         {                         ArrayListExample();         }         public static ArrayListArrayListExample()         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");                foreach (string  i in list)             {                 Console.WriteLine(i);             }               return list;         }     } 

7. ArrayList AddRange Method

You can use AddRange method to add two ArrayLists.Let’s look at below example for implementation.

C# Program to use AddRange:

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");               ArrayListlist2 = new ArrayList();             list2.Add("TechKatak");             list2.Add("ExamIron");               list.AddRange(list2);               foreach (string i in list)             {                 Console.WriteLine(i);             }         } } 

Output:

The above C# program will print below output in console:

Csharpstar

Easywcf

TechKatak

ExamIron

8. ArrayList Count and Clear Method

Count, Gets the number of elements actually contained in the ArrayList and Clear, clears the ArrayList. Let’s look at below example for Implementation.

  class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine(list.Count);             list.Clear();               foreach (string i in list)             {                 Console.WriteLine(i);             }         } } 

Output:

The above C# program will print below output in console:

4

9. ArrayList Sort and Reverse Method

Sort, gets the element actually sorted in ArrayList and Reverse, gets the ArryList elements in reverse order.LeImplementation.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");             //sort the ArrayList and print to Console             list.Sort();               foreach (string i in list)             {                 Console.WriteLine(i);             }                          //Reverse the ArrayList and print to Console             list.Reverse();               foreach (string i in list)             {                 Console.WriteLine(i);             }         } } 

Output:

The above C# program will print below output in console:

Csharpstar

Easywcf

ExamIron

TechKatak

TechKatak

ExamIron

Easywcf

Csharpstar

10. ArrayList Clone Method

Clone method of ArrayList creates a shallow copy of the ArrayList, means this Clone method simply create the copy of all element of the ArrayList.

    class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist is as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }             list.Clone();           Console.WriteLine("The shallow copy of arraylist is as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }                                } } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The shallow copy of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

11. ArrayList Contains Method

Contains method find whether the particular element exists in the ArrayList or not,if the element exists then it returns true otherwise returns false

class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               Console.WriteLine("The element Csharpstar contain in the ararylist is:" + list.Contains("Csharpstar"));             Console.WriteLine("The element MSDN contain in the ararylist is:" + list.Contains("MSDN"));             Console.WriteLine("The element TechKatak contain in the ararylist is:" + list.Contains("TechKatak"));                                } } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The element Csharpstar contain in the ararylist is:True

The element MSDN contain in the ararylist is: False

The element TechKatak contain in the ararylist is: True

12. ArrayList Insert Method:

Insert method inserts an element into the ArrayList at the specified index.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");             Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               list.Insert(0, "CsharpInsert");             list.Insert(3, "TechKatakInsert");             Console.WriteLine("The Elements of arraylist after Insert operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after Insert operation:

CsharpstarInsert

Csharpstar

Easywcf

TechKatak

TechKatakInsert

ExamIron

13. ArrayList InsertRange Method

IsertRange method, inserts range of elements to ArrayList at a perticular Index.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }             ArrayListlist1 = new ArrayList();             list1.Add("CsharpstarInsert");             list1.Add("TechKatakInsert");             list.InsertRange(3,list1);             Console.WriteLine("The Elements of arraylist after InsertRange operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after InsertRange operation:

Csharpstar

Easywcf

TechKatak

CsharpstarInsert

TechKatakInsert

Examiron

14. ArrayList Remove Method

Remove method removes the first occurrence of a specific object from the ArrayList.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               list.Remove("Easywcf");                        Console.WriteLine("The Elements of arraylist after Remove operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after Remove operation:

Csharpstar

TechKatak

Examiron

15. ArrayList RemoveAt Method

RemoveAt method removes the elements at the specified Index from the ArrayList.

class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               list.RemoveAt(1);                        Console.WriteLine("The Elements of arraylist after RemoveAt operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after RemoveAt operation:

Csharpstar

TechKatak

Examiron

16. ArrayList RemoveRange Method

RemoveRange method removes a range of elements from the ArrayList.

    class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               list.RemoveRange(1,2);                        Console.WriteLine("The Elements of arraylist after RemoveRange operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after RemoveRange operation:

Csharpstar

Examiron

17. ArrayList GetRange Method

GetRange method returns an ArrayList which represents a subset of the elements in the source ArrayList

class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }             ArrayListlist1 = list.GetRange(1,2);                        Console.WriteLine("The Elements of arraylist after GetRange operation:");               foreach (string i in list1)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after GetRange operation:

Easywcf

TechKatak

18. ArrayList SetRange Method

SetRange method inserts a collection to an ArrayList at specified Index.

class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               ArrayListlist1 = new ArrayList();             list1.Add("MSDN");               list.SetRange(2, list1);                        Console.WriteLine("The Elements of arraylist after SetRange operation:");               foreach (string i in list)             {                 Console.WriteLine(i);             }                                }       } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The Elements of arraylist after SetRange operation:

Csharpstar

Easywcf

MSDN

ExamIron

19. ArrayList IndexOf Method

IndexOf(Object) method searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The Elements of arraylist are as:");             foreach (string i in list)             {                 Console.WriteLine(i);             }               Console.WriteLine("The index value of Csharpstar is:" + list.IndexOf("Csharpstar"));             Console.WriteLine("The index value of TechKatak is:" + list.IndexOf("TechKatak"));                                }       } } 

Output:

The above C# program will print below output in console:

The Elements of arraylist is as:

Csharpstar

Easywcf

TechKatak

ExamIron

The index value of Csharpstar is: 0

The index value of TechKatak is: 2

20. Synchronized Method In ArrayList

The Synchronized(ArrayList) method of ArrayList returns a ArrayList wrapper that is synchronized (thread safe).

We pass the ArrayList in the Synchronized method and then check that it is Synchronize or not by using IsSynchronized property of ArrayList,Which returns the result in the form of true and false,if it is Synchronized then it return true otherwise false.Let’s look at below example for Implementation.

class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");             // Creates a Synchronized arraylist             ArrayListlist1 = ArrayList.Synchronized(list);             // Displays whether the ArrayList is Synchronized or not             Console.WriteLine("ArrayList list is {0}.", list.IsSynchronized ? "Synchronized" : "Not-Synchronized");             Console.WriteLine("ArrayList list1 is {0}.", list1.IsSynchronized ? "Synchronized" : "Not-Synchronized");                                          }       } 

Output:

The above C# program will print below output in console:

ArrayList list is Not-Synchronized

ArrayList list1 is Synchronized

21. ArrayList BinarySearch Method

BinarySearch method searches the element in the sorted arraylist and give the index value at which the element is found.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");               Console.WriteLine("The elements of the arraylist are:");             foreach (string i in list)             {                 Console.WriteLine(i);             }             list.Sort();             Console.WriteLine("After sorting the elements of the arraylist are:");             foreach (string i in list)             {                 Console.WriteLine(i);             }             Console.WriteLine("The element TechKatak searched at:" + list.BinarySearch("TechKatak"));             Console.WriteLine("The element ExamIron searched at:" + list.BinarySearch("ExamIron"));                                           }       } 

Output:

The above C# program will print below output in console:

The elements of the arraylist are:

Csharpstar

Easywcf

TechKatak

ExamIron

After sorting the elements of the arraylist are:

Csharpstar

Easywcf

ExamIron

TechKatak

The element TechKatak searched at: 3

The element ExamIron searched at: 2

22. Iterating over elements of an ArrayList

You can iterate over elements of an ArrayList using For Loop. Let’s look at below example for implementation.

 class CollectionExamples     {         static void Main(string[] args)         {             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");                        for (int i =0; i< list.Count ;i++)             {                 string value = list[i] as string;                 Console.WriteLine(value);             }                                          }       } 

Output:

The above C# program will print below output in console:

Csharpstar

Easywcf

TechKatak

ExamIron

23. Convert Array to ArrayList

 class CollectionExamples     {         static void Main(string[] args)         {             // Create the array             string[] array = { "Csharpstar", "Easywcf", "TechKatak", "ExamIron" };             Console.WriteLine("Enter the element of the array:");             for (int i = 0; i < 4; i++)             {                 Console.WriteLine(array[i]);             }             // Convert Array into ArrayList             ArrayListlist = new ArrayList(array);             Console.WriteLine("The elements in the arraylist that are copied from array:");             foreach (string s in list)             {                 Console.WriteLine(s);             }                                    }       } 

Output:

Enter the element of the array:

Csharpstar

Easywcf

TechKatak

ExamIron

The elements in the arraylist that are copied from array:

Csharpstar

Easywcf

TechKatak

ExamIron

24. Convert ArryList to Array

class CollectionExamples     {         static void Main(string[] args)         {             //create the arraylist             ArrayListlist = new ArrayList();             list.Add("Csharpstar");             list.Add("Easywcf");             list.Add("TechKatak");             list.Add("ExamIron");             Console.WriteLine("The elements of ArrayList are:");             Console.WriteLine();             foreach (string i in list)             {                 Console.WriteLine(i);             }             //create an array             string[] array = list.ToArray(typeof(string)) as string[];             Console.WriteLine();             Console.WriteLine("Arraylist elements are copied to array");             Console.WriteLine("The elements of the Array are :");             Console.WriteLine();             foreach (string valuein array)             {                 Console.WriteLine(value);             }               }       } 

Output:

The elements of ArrayList are:

Csharpstar

Easywcf

TechKatak

ExamIron

Arraylist elements are copied to array

The elements of the Array are :

Csharpstar

Easywcf

TechKatak

ExamIron

Thanks for Visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# ArrayList

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮