You are on page 1of 26

ArrayList

• An ArrayList represent a one-dimensional array whose


size is dynamically increased as required.

• It is a class present in the System.Collections.

• The capacity of an Array is fixed, whereas the capacity of


an ArrayList is automatically expanded as required.
• The initial capacity of ArrayList is 16.

• ArrayList provides methods that add, insert, or remove a


range of elements.
• Constructors:-
public ArrayList()
public ArrayList(int initialcapacity)
public ArrayList(ICollection c)
Adding objects to ArrayList
• Array list is a collection of objects.
• Objects can be added to the list using Add() method
• Syntax of Add method:-
public virtual int Add(object value)

• Example:-
ArrayList a=new ArrayList();
a.Add("chm");
a.Add(10);
a.Add("mahindra satyam");
a.Add(2.5);
foreach(object i in a)
{
System.Console.WriteLine(i);
}
AddRange() Method
• Syntax:- public virtual void AddRange( ICollection c)
• Adds the elements of an ICollection to the end of the ArrayList.

using System.Collections;
class test {
public static void Main() {
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.AddRange(x);
for(int i=0;i<a.Count;i++)
System.Console.WriteLine(a[i]);
} }
ArrayList Indexer
• There is a read-write indexer associated with an ArrayList.

• So the following code is valid in C#.

ArrayList a=new ArrayList();


a.Add("chm");
System.Console.WriteLine(a[0]);
CopyTo() Method
• This overloaded method copies the ArrayList or a portion of it to
a one-dimensional array.

• public virtual CopyTo(Array target)


• public virtual CopyTo(Array target,int start)
• public virtual CopyTo(int ss,Array target,int ts,int length)

• Example:-
*See next slide
Sample Program
using System.Collections; using System;
class test {
public static void Main() {
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.AddRange(x);
Array ta=Array.CreateInstance(typeof(object),5);
ta.SetValue("one",0);
ta.SetValue("two",1);
ta.SetValue("three",2);
a.CopyTo(3,ta,2,3);
for(int i=0;i<ta.Length;i++)
System.Console.WriteLine(ta.GetValue(i));
} }
Insert() Method
• Inserts an element into the ArrayList at the specified index.
• Syntax:- public virtual void Insert(int index, object value)
• Example:-
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.Insert(2,“an example");
for(int i=0;i<a.Count;i++)
System.Console.WriteLine(a[i]);
Output
this
is
an example
InsertRange() Method
• Inserts the elements of a collection into the ArrayList at the
specified index.
• public virtual void InsertRange( int index, ICollection c)
Example:-
int[] x={55,33,44,90};
double[] y={2.5,3.5,4.5,5.5};
ArrayList a=new ArrayList();
a.AddRange(x);
a.InsertRange(2,y);
foreach(object i in a)
System.Console.WriteLine(i);
GetEnumerator() Method
• Public virtual IEnumerator GetEnumerator()

IEnumerator supports a simple iteration over a collection.

(See Next Slide)


Iterating A Collection Using
IEnumerator
int[] x={55,33,44,90};
double[] y={2.5,3.5,4.5,5.5};
ArrayList a=new ArrayList();
a.AddRange(x);
a.InsertRange(2,y);
IEnumerator en=a.GetEnumerator();
while(en.MoveNext())
{
System.Console.WriteLine(en.Current);
}
Remove() & RemoveAt()
• Remove() method removes the first occurrence of a specific object
from an ArrayList.
• public virtual void Remove( object obj )
• RemoveAt() method removes an object at a specific index from an
ArrayList.
• public virtual void RemoveAt( int index )
Example:-
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.AddRange(x);
a.Remove(44);
a.RemoveAt(2);
RemoveRange() Method
• It Removes a range of objects from an ArrayList

• public virtual void RemoveRange( int index, int count)

• Example:-
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.AddRange(x);
a.RemoveRange(1,3);
ReadOnly() & FixedSize()
• public static ArrayList ReadOnly(ArrayList)
• public static ArrayList FixedSize(ArrayList)
Example:-
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.AddRange(x);
ArrayList b=ArrayList.FixedSize(a);
ArrayList c=ArrayList.ReadOnly(a);
//b.Add(22); Error because the array list b is fixed size
//c[1]=100; Error because the array list c is read-only
IEnumerator en=b.GetEnumerator();
while(en.MoveNext()) {
System.Console.WriteLine(en.Current); }
Contains() & BinarySearch()
Contains
• Determines whether an element is in the ArrayList.
Syntax:-
public virtual bool Contains(object item)

BinarySearch

• Searches a value from a sorted one-dimensional array using


binary search algorithm
Syntax:-
public virtual int BinarySearch(object)
Sample Program
using System.Collections;
class test {
public static void Main() {
ArrayList a=new ArrayList();
int x=15;
for(int i=0;i<17;i++)
{
a.Add(x);
x--;
System.Console.WriteLine(a[i]);
}
System.Console.WriteLine(a.Contains(10));
a.Sort();
System.Console.WriteLine(a.BinarySearch(10));
} }
Function Pointer
# include <iostream.h>
# include <conio.h>
void test(int x)
{
cout<<x*x;
}
void main()
{
void (*p)(int);
p=test;
p(100);
}
Delegates
• A delegate is a reference type that references a method.

• A delegates refers to either an instance method or a static method.

• Delegates are similar to function pointers in C++.


Following are the steps in
creating a delegate

• Delegate declaration
• Delegate method definition
• Delegate instantiation
• Delegate invocation
Delegate Declaration
modifier delegate return-type delegate-name (formal-parameters);

where modifier specifies the accessibility of the delegate,

delegate is a keyword,

delegate-name is any valid C# identifier,


formal-parameter defines the parameter list.

Examples:-
public delegate void mydelegate ();
public delegate int delexample (int x,int y);
Delegate Method Definition
• The methods that are referenced by a delegate are known as delegate
methods.
• The signature of a delegate method must be same as that of its
delegate.
• Syntax:-
modifier delegate return-type delegate-name (formal-parameters)
{
Statements;
}
• Example:-
delegate int mydelegate(); // Delegate declaration
public int delmethod1(int x,int y){} //Delegate method 1
public int delmethod2(int x,int y){} //Delegate method 2
Delegate instantiation
• Creation of a delegate instance takes the form:

• delegatetype object= new delegatetype(invocation-target-


expression )

• Example:-

• mydelegate m1=new mydelegate(classname.methodname);


//if it is static method
• mydelegate m=new mydelegate(object.methodname ); //if it is
//instance method
Delegate invocation

• Syntax:-

delegate-objectname (parameters list);


Sample Program 1
using System;
public delegate void mydelegate();
class test {
public static void method1() {
System.Console.WriteLine( "first delagate method");
}
public static void method2() {
System.Console.WriteLine( "second delagate method");
}
public static void Main()
{
mydelegate m1=new mydelegate(test.method1);
mydelegate m2=new mydelegate(test.method2);
m1();
m2();
} }
Sample Program 2
using System;
public delegate void mydelegate();
class test {
public void method1() {
System.Console.WriteLine( "first delagate method");
}
public void method2() {
System.Console.WriteLine( "second delagate method");
}
public static void Main() {
test t=new test();
mydelegate m1=new mydelegate(t.method1);
mydelegate m2=new mydelegate(t.method2);
m1();
m2();
} }
Delegate Multicasting
• If a single delegate is created out of multiple
delegates resulting in delegate multicasting.

• Example:-
mydelegate m1=new mydelegate(t.method1);
mydelegate m2=new mydelegate(t.method2);
mydelegate m3=m1+m2; //delegate multicasting
m3() //multicasted delegate invocation
Sample Program
using System;
public delegate void mydelegate();
class test {
public void method1() {
System.Console.WriteLine( "first delagate method");
}
public void method2() {
System.Console.WriteLine( "second delagate method");
}
public static void Main() {
test t=new test();
mydelegate m1=new mydelegate(t.method1);
mydelegate m2=new mydelegate(t.method2);
mydelegate m3=m1+m2+m1+m2+m2-m1;
m3();
} }

You might also like