Hi Guys
NP98 Remove(x) & RemoveAt(0)
In the following program activation of Remove(x) is producing //1 2 output. Activation of RemoveAt(0) is producing //2 3 output.
Please explain the reason.
Thank you
using System;
using System.Collections;
class MainClass
{
static void
{
ArrayList a = new ArrayList(10);
int x = 0;
a.Add(++x);
a.Add(++x);
a.Add(++x);
a.Remove(x);
//a.RemoveAt(0);
foreach (int i in a)
Console.Write(i + " ");
}
}
//1 2
using System;
using System.Collections;
class MainClass
{
static void
{
ArrayList a = new ArrayList(10);
int x = 0;
a.Add(++x);
a.Add(++x);
a.Add(++x);
//a.Remove(x);
a.RemoveAt(0);
foreach (int i in a)
Console.Write(i + " ");
}
}
//2 3
Posted Apr 23, 2008, 7:39 PM
Thank you for your explanation, Alan
AlanPosted Apr 23, 2008, 6:46 PM
In both cases the ArrayList contains the following integers:
index 0 : 1
index 1 : 2
index 2 : 3
In the first program Remove(x) removes the item at index 2, because x is currently 3. Thus only 1 and 2 are left.
In the second program, RemoveAt(0) removes the item at index 0 which is 1. Thus only 2 and 3 are left.