How do you remove an object from ArrayList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why doesn't the line below work in the code:
aList.Remove(new TestItem(3, "test3")); // <---- why doesn't this work?

Code follows below.

Output results:
Default ArrayList capacity: 16 [Create List] [Try to remove 3]Output of
Results after removal attempt: [0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 -
11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - ][removal of 3 failed because it
appears]


private void Form1_Load(object sender, System.EventArgs e)
{
/*
* Code to demonstrate using the ArrayList
*/
ArrayList aList = new ArrayList();

// Show the default capacity
textBox1.Text = "Default ArrayList capacity: " +
aList.Capacity.ToString() + " ";

textBox1.Text = textBox1.Text + "[Create List] ";
// Add some numbers to the list
for( int i = 0; i < 20; i++ )
{
aList.Add(new TestItem(i, "test" + i.ToString()));
}

textBox1.Text = textBox1.Text + "[Try to remove 3]";
aList.Remove(new TestItem(3, "test3")); // <---- why doesn't this work?

// Retrieve the enumerator and use it to loop the list

textBox1.Text = textBox1.Text + "Output of Results after removal attempt:
[";
IEnumerator listEnumerator = aList.GetEnumerator();
while( listEnumerator.MoveNext() )
{
textBox1.Text = textBox1.Text + String.Format("{0} - ", ((TestItem)
(listEnumerator.Current)).ID.ToString());
}
textBox1.Text = textBox1.Text + "]";
textBox1.Text = textBox1.Text + "[removal of 3 failed because it appears]";

}

public class TestItem
{
private int _ID = 0;
private string _desc = "";

public TestItem(int id, string desc)
{
_ID = id;
_desc = desc;
}

public int ID
{
get { return this._ID; }
set { this._ID = value; }
}

public string desc
{
get { return this._desc; }
set { this._desc = value; }
}
 
The problem is that the arraylist calls object.equals method to remove the
item. It will work fine if you override the Equals function of your TestItem
class.

Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
' Not a test item element, not equal
If (Not (TypeOf obj Is TestItem)) Then
Return False
End If
Dim ti As TestItem = CType(obj, TestItem)
Return (ti.ID.Equals(ID) AndAlso ti.Desc.Equals(Desc))
End Function

Jared
 
Thanks! I'll try that out.

Jared said:
The problem is that the arraylist calls object.equals method to remove the
item. It will work fine if you override the Equals function of your TestItem
class.

Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
' Not a test item element, not equal
If (Not (TypeOf obj Is TestItem)) Then
Return False
End If
Dim ti As TestItem = CType(obj, TestItem)
Return (ti.ID.Equals(ID) AndAlso ti.Desc.Equals(Desc))
End Function

Jared

MisterKen said:
Why doesn't the line below work in the code:
aList.Remove(new TestItem(3, "test3")); // <---- why doesn't this work?

Code follows below.

Output results:
Default ArrayList capacity: 16 [Create List] [Try to remove 3]Output of
Results after removal attempt: [0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -
10 -
11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - ][removal of 3 failed because
it
appears]


private void Form1_Load(object sender, System.EventArgs e)
{
/*
* Code to demonstrate using the ArrayList
*/
ArrayList aList = new ArrayList();

// Show the default capacity
textBox1.Text = "Default ArrayList capacity: " +
aList.Capacity.ToString() + " ";

textBox1.Text = textBox1.Text + "[Create List] ";
// Add some numbers to the list
for( int i = 0; i < 20; i++ )
{
aList.Add(new TestItem(i, "test" + i.ToString()));
}

textBox1.Text = textBox1.Text + "[Try to remove 3]";
aList.Remove(new TestItem(3, "test3")); // <---- why doesn't this work?

// Retrieve the enumerator and use it to loop the list

textBox1.Text = textBox1.Text + "Output of Results after removal attempt:
[";
IEnumerator listEnumerator = aList.GetEnumerator();
while( listEnumerator.MoveNext() )
{
textBox1.Text = textBox1.Text + String.Format("{0} - ", ((TestItem)
(listEnumerator.Current)).ID.ToString());
}
textBox1.Text = textBox1.Text + "]";
textBox1.Text = textBox1.Text + "[removal of 3 failed because it
appears]";

}

public class TestItem
{
private int _ID = 0;
private string _desc = "";

public TestItem(int id, string desc)
{
_ID = id;
_desc = desc;
}

public int ID
{
get { return this._ID; }
set { this._ID = value; }
}

public string desc
{
get { return this._desc; }
set { this._desc = value; }
}
 
Back
Top