Advertisement



< Prev
Next >



C# ArrayList Class





ArrayList is a non-generic collection class which is used to store a collection of objects. The ArrayList collection class that implements IList, ICollection, IEnunerable, ICloneable interfaces and it is defined in the System.Collections.Generic namespace.

Contrary to a standard array, where we must declare its size before we use it and once its size is declared, it's fixed, an ArrayList is like a dynamic array i.e. we don't need to declare its size, it grows as we add elements to it and it shrinks as we remove elements from it, during the runtime of the program.









Constructors of ArrayList:


Constructors Description
ArrayList()
This constructor of ArrayList creates an empty ArrayList and has the default initial capacity.

The capacity of an ArrayList is the number of elements it can hold, as the new elements are added to the ArrayList, its capacity is automatically increased.

ArrayList(ICollection)
This constructor creates an ArrayList initialized with the elements of an ICollection.

ArrayList(int capacity)
This constructor initializes created an ArrayList that is empty and has the specified initial capacity..






Properties of ArrayList:


Properties Description
Capacity
This property is used to get or set the total number of elements an ArrayList can hold.

Count
This property gives total number of elements in an ArrayList.

IsFixedSize
This property determines whether an ArrayList has a fixed size or not.

IsReadOnly
This property determines whether an ArrayList is read-only or not.

IsSynchronzied
This property determines whether an ArrayList is thread-safe or not.

Item[int index]
This property gets or sets an item of ArrayList at a specified index.






Some commonly used methods of ArrayList class


Methods Description
int Add(Object ob)
This method adds the specified object at the end of an ArrayList and returns its index.

bool Contains(Object o)
This method checks if ArrayList contains a specific Object.

void Clear()
This method removes all of the elements from the ArrayList.

IEnumerator GetEnumerator()
This method returns to enumerate the entire ArrayList.

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

int LastIndexOf(Object o)
This method searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.

void Remove(Object o)
This method removes the first occurrence of an Object from the ArrayList, if it is present.

void RemoveAt(int index)
This method removes an element at a specified index in an ArrayList.

Object[] ToArray()
This method returns an Object array containing all the elements of an ArrayList.

void Sort()
This method sorts an ArrayList.

void Reverse()
This method reverses the order of the elements of an ArrayList.



Advertisement




Creating a ArrayList and displaying its features.




//C# Example of ArrayList


using System;
using System.Collections;

class ArrayList1
{
public static void Main(String[] ar)
{
	//Creating an ArrayList
	ArrayList arr= new ArrayList();
	
	//Printing the initial capacity of an ArrayList
	Console.WriteLine("Initial capacity of ArrayList = "+ arr.Capacity);

	//Adding elements to an ArrayList by Add() method
	arr.Add("C");
	arr.Add('A');
	arr.Add(14.4f);
	arr.Add("F");
	arr.Add('x');
	arr.Add(1);

	//Printing the elements of an ArrayList, using for loop
	Console.Write("ArrayList after adding objects: ");
	for(int i=0;i<arr.Count;i++)
		Console.Write(arr[i]+ " ");

	//Printing the total number of elements in an ArrayList.
	Console.WriteLine("\nSize of ArrayList = "+ arr.Count);

	//Removing an element at the 4th index of ArrayList
	arr.RemoveAt(4);	

	//Removing an element "1" of the ArrayList
	arr.Remove("1"); 

	//Printing the elements of an ArrayList, using foreach loop
	Console.Write("An updated ArrayList: ");
	for(int i = 0; i<arr.Count; i++)
		Console.Write(arr[i] + " ");

	//Printing the total number of elements in an ArrayList.
	Console.WriteLine("\nSize of  ArrayList = "+ arr.Count);

	//Printing the current capacity of an ArrayList
	Console.WriteLine("Current capacity of ArrayList = "+ arr.Capacity);
}
}


Output is - :


Initial capacity of ArrayList = 0
ArrayList after adding objects: C A 14.4 F x 1
Size of ArrayList = 6
An updated ArrayList: C A 14.4 F 1
Size of  ArrayList = 5
Current capacity of ArrayList = 8


Program Analysis







Iterating over ArrayList within foreach loop.


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement