ArrayList
:-
It is an alternative to array. However unlike
array you can add and remove items from a list of specified postion using an index
and the array resize itself automatically. It allows dynamic memory allocation,
Add, search and sort items in the list.
Properties
of ArrayList :-
Capacity :- Gets or sets the number of elements that ArrayList
can contain.
Count :- Gets the number of elements actually contained
thr ArrayList.
IsFixedSize :- Gets a value indicating whether the ArrayList
has fixed size.
IsReadOnly :- Gets
a value indicating whether the ArrayList has ReadOnly.
Item :- Gets or sets the elements at specified
index.
Methods
of ArrayList :-
There
are 15 methods in Arraylist i.e Add(),Clear(),AddRange() which are explained in
example. Below example has given in which all methods have used.
Example:-
using System;
using
System.Collections;
using
System.Linq;
using
System.Text;
using
System.Data;
namespace
CollectionExample
{
class Program
{
static void Main(string[]
args)
{
//
ArrayList Example
---------------------------------------------------------------
ArrayList
arrayList = new ArrayList();
Console.WriteLine("Adding some Number.");
arrayList.Add(12);
arrayList.Add(34);
arrayList.Add(54);
arrayList.Add(23);
arrayList.Add(65);
arrayList.Add(33);
arrayList.Add(89);
arrayList.Add(39);
Console.WriteLine("Capacity :{0}", arrayList.Capacity);
Console.WriteLine("Count :{0}", arrayList.Count);
Console.Write("Content: ");
foreach
(int i in
arrayList)
{
Console.Write(i
+ " ");
}
Console.WriteLine();
Console.Write("Sorted Content: ");
arrayList.Remove(12);//remove item 12
arrayList.RemoveAt(1);//remove item from specified position using index
arrayList.RemoveRange(2, 4);//remove no of item from specified position
arrayList.Sort();// sorting arrayList
foreach
(int i in
arrayList)
{
Console.Write(i
+ " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Output:-
Adding some Number.
Capacity :8
Count :7
Content:12 34 54 23 65 33 89 39
Sorted Content: 23 34
No comments:
Post a Comment
Note: only a member of this blog may post a comment.