Reputation: 11
I can't get around this problem: How do I remove a string array from a list?
So the code is as follows:
List<string[]> theList = new List<string[]>();
string[] myStringarray = new string[] { a, b, c };
theList.Add(myStringarray);
The problem here is that I want the user to be able to create new strings for the string array, which then is going to be put in the List - this is already solved as you can see above.
However, how do I remove one of the string arrays in the list? I have tried transforming the List to a Jagged Array, and a couple of methods, but I just can't seem to find a solution to this particular problem.
Upvotes: 0
Views: 4959
Reputation: 16261
You can simply use Remove
function:
List<string[]> theList = new List<string[]>();
string[] myStringarray = new string[] { a, b, c };
theList.Add(myStringarray);
theList.Remove(myStringarray);
Remove() Removes the the specified element.
RemoveAt() Removes the element at the specified index.
RemoveAll() Removes all the elements that match with the supplied predicate function.
If you want to check if the arrays are match. you can use:
for(int i=0;i<theList.Count();i++)
{
bool areEqual =theList[i].SequenceEqual(newStringArray);
if(areEqual)
theList.RemoveAt(i);
}
Upvotes: 0
Reputation: 108
For the case RemoveAll with a predicate I offerthe following (removing by avalue in arrays)
List<string[]> theList = new List<string[]>();
string[] myStringarray = new string[] { "a", "b", "c" };
theList.Add(myStringarray);
theList.Add(new string[] { "d", "e", "f" });
theList.RemoveAll(zz => zz.Where(xx => xx == "b").Count() > 0);
Upvotes: 0
Reputation: 575
You can use Remove() method if you already have a reference to the string array that you want to remove like the following:
List<string[]> theList = new List<string[]>();
string[] myStringarray = new string[] { "a", "b", "c" };
theList.Add(myStringarray);
//remove myStringarray from the main list
theList.Remove(myStringarray);
However if you are getting the items from the user and you want to search for an array that contains these elements and remove them, I recommend creating an extension method like the following:
public static class ExtensionMethods
{
public static string[] FindAndRemove(this ICollection<string[]> list, string[] items)
{
string[] removedList = null;
foreach (var stringArray in list)
{
if (stringArray.SequenceEqual(items))
{
removedList = stringArray;
break;
}
}
if (removedList != null)
{
list.Remove(removedList);
}
return removedList;
}
}
Which is mainly search for the first array that its elements equal the passed element in item array (the parameter) and remove it, you can further improve this method and make it remove all the lists that satisfy the condition as following:
public static class ExtensionMethods
{
public static int FindAndRemove(this List<string[]> list, string[] items)
{
return list.RemoveAll(arr => arr.SequenceEqual(items));
}
}
Here I have used RemoveAll from Linq library, which remove all the lists that meet the given condition. Note that SequecnceEqual also exists inside linq library and used to:
Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
Upvotes: 3
Reputation: 72
Take iterator .........
Block quote
Iterator itr =list.iterator(); while(itr.hasNext()){ //some condition itr.remove(); }
Block quote
.......
Upvotes: -2
Reputation: 299
I would use List.RemoveAll (https://msdn.microsoft.com/en-us/library/wdka673a(v=vs.110).aspx)
Remove all takes a predicate function which will allow you to find any items you want to remove.
For example if the containing array has a specific value in it.
Upvotes: 0