Kai77
asked on
Removing elements from arraylist
I have a arraylist arrHighlightIDs which for instance can be something like (67,67,68,68,68,100,101,10 1)
I want too remove all the elements with the same value (e.g. 68, his variable is randomly generated) so I wrote the following code:
iNumberOfTimes = 0;
foreach(object item in arrHighlightIDs)
{
if (item.ToString() == arrHighlightIDs[iRandValue ])
{
iNumberOfTimes = iNumberOfTimes + 1;
}
}
Response.Write("Number of times to be deleted: " + iNumberOfTimes);
while (iNumberOfTimes > 0)
{
arrHighlightIDs.Remove(arr HighlightI Ds[iRandVa lue]);
iNumberOfTimes = iNumberOfTimes - 1;
}
First of all this does not work, second of all, there must be an easier way to delete multiple elements with the same value from an arraylist at once. Any ideas?
Thanks
I want too remove all the elements with the same value (e.g. 68, his variable is randomly generated) so I wrote the following code:
iNumberOfTimes = 0;
foreach(object item in arrHighlightIDs)
{
if (item.ToString() == arrHighlightIDs[iRandValue
{
iNumberOfTimes = iNumberOfTimes + 1;
}
}
Response.Write("Number of times to be deleted: " + iNumberOfTimes);
while (iNumberOfTimes > 0)
{
arrHighlightIDs.Remove(arr
iNumberOfTimes = iNumberOfTimes - 1;
}
First of all this does not work, second of all, there must be an easier way to delete multiple elements with the same value from an arraylist at once. Any ideas?
Thanks
//arrHighlightIDsis the actual source list
//arrHighlightIDs2 is another that i have created that is used to contain non-redundant data
ArrayList arrHighlightIDs2 = new ArrayList();
for (int i =0; i<arrHighlightIDs.Count;i+ +)
{
if (arrHighlightIDs2.Contains (Convert.T oDouble(ar rHighlight IDs[i]))== false)
{
arrHighlightIDs2.Add((Conv ert.ToDoub le(arrHigh lightIDs[i ])));
}
}
arrHighlightIDs= arrHighlightIDs2;
//arrHighlightIDs2 is another that i have created that is used to contain non-redundant data
ArrayList arrHighlightIDs2 = new ArrayList();
for (int i =0; i<arrHighlightIDs.Count;i+
{
if (arrHighlightIDs2.Contains
{
arrHighlightIDs2.Add((Conv
}
}
arrHighlightIDs= arrHighlightIDs2;
ASKER
Sorry, arrHighlightsIDs is a stringcollection (instead of an arraylist).
So the following error occurs:
Cannot implicitly convert type 'System.Collections.ArrayL ist' to 'System.Collections.Specia lized.Stri ngCollecti on'
Anyway to convert the arrHighlightIDs2 into a stringcollection?
So the following error occurs:
Cannot implicitly convert type 'System.Collections.ArrayL
Anyway to convert the arrHighlightIDs2 into a stringcollection?
try this
System.Collections.Special ized.Strin gCollectio n arrHighlightIDs2 = new System.Collections.Special ized.Strin gCollectio n ();
for (int i =0; i<arrHighlightIDs.Count;i+ +)
{
if (arrHighlightIDs2.Contains (Convert.T oString(ar rHighlight IDs[i]))== false)
{
arrHighlightIDs2.Add((Conv ert.ToStri ng(arrHigh lightIDs[i ])));
}
}
arrHighlightIDs= arrHighlightIDs2;
System.Collections.Special
for (int i =0; i<arrHighlightIDs.Count;i+
{
if (arrHighlightIDs2.Contains
{
arrHighlightIDs2.Add((Conv
}
}
arrHighlightIDs= arrHighlightIDs2;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://www.experts-exchange.com/questions/20666267/Remove-redundant-value-of-arraylist.html#8847983
someone has answered this before!!