How to use arraylist in c#? [Resolved]

Posted by Susanthampy under C# on 6/10/2011 | Points: 10 | Views : 52403 | Status : [Member] [MVP] | Replies : 10
How to use arraylist in c#....

Give some examples....

Regards,
Susan



Responses

Posted by: Annupandey on: 6/15/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
ArrayList is a class that t belongs to System.Collection.It can accept all the data types into it..It is defined by four blocks by default.
eg.
class c
{
Static void Main(String [ ]args)
{
ArrayList ar = new ArrayList();

ar.add("rek");

ar.add(3);

ar.add(33.3f);

Console.Write(ar);

}
}

output-
rek
3
33.3


Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 6/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Susan,

See below example about arraylist .

using System;

using System.Collections;

class Program
{
static void Main()
{
//
// Create an ArrayList with two values.
//
ArrayList list = new ArrayList();
list.Add(3);
list.Add(4);
//
// Second ArrayList.
//
ArrayList list2 = new ArrayList();
list2.Add(5);
list2.Add(6);
//
// Add second ArrayList to first.
//
list.AddRange(list2);
//
// Display the values.
//
foreach (int i in list)
{
Console.WriteLine(i);
}
}
}

Output

3
4
5
6



GetRanage


using System;

using System.Collections;

class Program
{
static void Main()
{
//
// Create an ArrayList with 4 strings.
//
ArrayList list = new ArrayList();
list.Add("DotNet");
list.Add("Java");
list.Add("PHP");
list.Add("SQL");
//
// Get last two elements in ArrayList.
//
ArrayList range = list.GetRange(2, 2);
//
// Display the elements.
//
foreach (string val in range)
{
Console.WriteLine(val);
}
}
}
OutPut:
PHP
SQL


same way have a look at all methods of arraylist.

Regards,
Sunil

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 6/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Susan,
Refer,
http://www.dotnetperls.com/arraylist

Nitha Deepak

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Sunil

thanks for ue reply

Regards,
Susan

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Nitha,

thanks for ur reply

Regards,
Susan

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/15/2011 [Member] Starter | Points: 25

Up
0
Down
try this

http://www.java2s.com/Code/CSharp/Collections-Data-Structure/DemonstrateArrayList.htm

Thanks&Regards
LakshmiNarayana Nalluri.

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/15/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Annu,

Thanks for ur reply

Regards,
Susan

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/15/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Lakn,

thanks for ur reply

Regards,
Susan

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajashekaranmec on: 6/28/2012 [Member] Starter | Points: 25

Up
0
Down
ArrayList list = new ArrayList();
list.Add("rajan");
list.Add(20000);
string[] stringList = list.OfType<string>().ToArray();
Int32[] intList = list.OfType<Int32>().ToArray();
int n1 = intList[0];
Console.WriteLine(n1);

output :20000

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aksrinivas84 on: 7/11/2012 [Member] Starter | Points: 25

Up
0
Down
//First of all create an object for the ArrayList Class

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "My" );
myAL.Add( "Name" );
myAL.Add( "is" );
myAL.Add( "Srinivas" );
//Now once the list is created try to display the list
foreach (string val in myAL)
{
Console.WriteLine(val);
}

Susanthampy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response