To Remove even number from ArrayList in C#

Posted by Ramsakthi under C# on 6/9/2012 | Points: 10 | Views : 2832 | Status : [Member] | Replies : 2
Hi,
how to remove even number from ArrayList in C#.

Regards,
RamuSakthiVel,

Ramu.R


Responses

Posted by: Nrnr6 on: 6/9/2012 [Member] Starter | Points: 25

Up
0
Down
ArrayList numbers = new ArrayList();
for(int i=1;i<11;i++)
numbers.Add(i);
numbers=filter(numbers);


public ArrayList filter(ArrayList a)

{
ArrayList b = new ArrayList();
foreach (int n in a)
{
if (n % 2 == 0)
{ }
else
b.Add(n);
}
return b;
}


nethaji

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

Posted by: CGN007 on: 6/11/2012 [Member] Silver | Points: 25

Up
0
Down
Removing even number position elements from ArrayList in C#
using System;

using System.Collections;

namespace StudyBasics
{
class Program
{
static void Main(string[] args)
{
//Declaring the ArrayList
ArrayList arrayList = new ArrayList();

//Adding the element 1-10 in the arraylist
for (int i = 1; i < 11; i++)
arrayList.Add(i);

// remove even number positions from the arraylist
for (int i = 2; i < 11; i += 2)
arrayList.Remove(i);

// Printing the arraylist elemets.Now it only contains the odd number position elements.
foreach (int i in arrayList)
Console.WriteLine(i);

Console.ReadKey();
}
}
}


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

Login to post response