C# List - Removing items while looping / iterating [duplicate] - c#

This question already has answers here:
How to remove elements from a generic list while iterating over it?
(28 answers)
Closed 5 years ago.
Suppose that I have the following code snippet:
var data=new List<string>(){"One","Two","Three"};
for(int i=0 ; i<data.Count ; i++){
if(data[i]=="One"){
data.RemoveAt(i);
}
}
The following code throws exception.
My question is what is the best way to avoid this exception and to remove the element while looping?

If you need to remove elements then you must iterate backwards so you can remove elements from the end of the list:
var data=new List<string>(){"One","Two","Three"};
for(int i=data.Count - 1; i > -1; i--)
{
if(data[i]=="One")
{
data.RemoveAt(i);
}
}
However, there are more efficient ways to do this with LINQ (as indicated by the other answers).

You can use List<T>.RemoveAll to handle this:
data.RemoveAll(elem => elem == "One");

You can also use a forward moving loop like:
var data = new List<string>() { "One", "Two", "Three", "One", "One", "Four" };
for (int i = 0; i < data.Count; i++)
{
if (data[i] == "One")
{
data.RemoveAt(i--);
}
}
This line data.RemoveAt(i--); is stopping the effect of increment in the iteration variable at the end of the loop, in case of item being removed from the list.
It will remove the item from index at the current iteration value and then after removing the item, the iterator would be set to one less value than the current one. At the end of loop, the increment in loop body will move it to next valid index.
Here is a working dotfiddle
(Please note, that I personally use reverse loop for situation like these because IMO, they are easier to understand, this answer here is just for displaying another way of achieving it).

I happen to come across a simple solution for this using foreach and .ToArray()
var data=new List<string>(){"One","Two","Three"};
foreach ( var d in data.ToArray()){
if(d =="One"){
data.Remove(d);
}
}

You could try ChrisF's reverse iteration method to remove your item.
You could also simply:
List.Remove("One");
Or:
List.RemoveAll(i => i == "One"); // removes all instances
And be done with it. There's really no point in iterating over the collection to remove a single item.

Why don't you just simply decrement the iterator variable?
var data=new List<string>(){"One","Two","Three"};
for(int i=0 ; i<data.Count ; i++){
if(data[i]=="One"){
data.RemoveAt(i);
i--; // <<<<<<<<<<<
}
}

Here is a dirty trick and I wonder what the critique of it will be?
var data=new List<string>(){"One","Two","Three"};
foreach (string itm in (data.ToArray()))
{
if string.Compare(name, "one", true) == 0) data.Remove(name);
}

var data=new List<string>(){"One","Two","Three"};
for(int i=0; i<data.Count; ){
if(data[i]=="One") data.RemoveAt(i);
else ++i;
}

The general solution below makes a copy of the list and handles the negative index:
foreach (void item_loopVariable in MyList.ToList) {
item = item_loopVariable;
}

var data = new List<string>() { "One", "Two", "Three" };
data.RemoveAll(p=>p=="One");

You can use Stack class
Stack<string> myStack = new Stack<string>();
foreach (var item in Enumerable.Range(1,1001))
myStack.Push("Str " + item.ToString());
while (myStack.Any())
Console.WriteLine("Now {0} items in Stack, removed item is {1}",myStack.Count,myStack.Pop());
Console.ReadKey();

I had to remove more than one item from the list. so,I reinitialized the list count. Is there any other better option?
for (int i = dtList.Count - 1; dtList.Count > 0; )
{
DateTime tempDate = dtList[i].Item1.Date;
var selectDates = dtList.FindAll(x => x.Item1.Date == tempDate.Date);
selectDates.Sort((a, b) => a.Item1.CompareTo(b.Item1));
dtFilteredList.Add(Tuple.Create(selectDates[0].Item1, selectDates[0].Item2));
dtList.RemoveAll(x => x.Item1.Date == tempDate.Date);
i = dtList.Count - 1;
}

List<string> list = new List<string>();
list.Add("sasa");
list.Add("sames");
list.Add("samu");
list.Add("james");
for (int i = list.Count - 1; i >= 0; i--)
{
list.RemoveAt(i);
}

Related

Get listitems according to the index of another list

I have a List<List<string>> with three nested lists. Now I need to check if List[1] equals a certain string and if so, check if the value at this index in List[2] has another certain string. If both conditions return true, then I need to get that certain index and get the item of List[0].
For example:
var list = Titles[0];
var list2 = Titles[1];
var list3 = Titles[2];
foreach (var item in list2)
{
if (item.Contains("Dt."))
{
int idx = list2.IndexOf(item);
var value = list3.ElementAt(idx);
if (value.Contains("25.04.2017"))
{
var newList = list.ElementAt(idx);
}
}
}
This approach doesn't seem very efficient in regards to performance, especially if the nested list contains ~9000 items.
I tried to get the result via lambda expressions first, but I'm not sure if this is the right approach either.
What would be the best or most efficient solution?
Eliminate ElementAt with direct access to index. I believe ElementAt iterates over List in order to get i'th element
Eliminate usage of IndexOf with index provided by for loop I believe IndexOf iterates over List in order to find matching element.
var list = Titles[0];
var list2 = Titles[1];
var list3 = Titles[2];
for (int i = 0 ; i < list2.Count; ++ i)
{
var item = list2[i];
if (item.Contains("Dt."))
{
var value = list3[i];
if (value.Contains("25.04.2017"))
{
var newList = list[i];
}
}
}
Note if size of list2 is greater than size of list or list3 then you potentially get IndexOutOfRangeException
Lambda equivalent for your code:
if(list2.Any(item => item.Contains("Dt.")))
{
int idx = list2.IndexOf("Dt.");
if(list3.ElementAt(idx).Contains("25.04.2017"))
{
var newList = list.ElementAt(idx);
}
}
for (int i = 0; i < list2.Count; ++i)
{
var item = list2[i];
if (item.Contains("Dt."))
{
var value = list3[i];
if (value.Contains("25.04.2017"))
{
var newList = list[i];
break; // Break the loop :-)
}
}
}

Handling multiple lists with if/else condition - recursion

i have many lists which contain 1 or more objects, and if there is more then 1 object in the list, it should do something. When there is only 1 object in the list, then take the second list and do the same. This should happen for 4 lists.
This is my Solution, but i think this is a bad solution. Is there a far better way on handling this code?
if (list1.Count > 1)
for (int i = 0; i < list1.Count; i++)
{
DoSomething(list1);
}
else
{
if (list2.Count > 1)
for (int i = 0; i < list2.Count; i++)
{
DoSomething(list2);
}
else
{
if (list3.Count > 1)
for (int i = 0; i < list3.Count; i++)
{
DoSomething(list3);
}
else
{
...
Many thanks and best regards,
Joerg
// using System.Collections.Generic;
// using System.Linq;
// create an enumerable "list" of your lists (however many there are):
var lists = new[] { list1, list2, list3, … };
// find the first list that has more than 1 element:
var list = lists.FirstOrDefault(_ => _.Count() > 1);
// if there is such a list…
if (list != null)
{
// … then `DoSomething` to each of its items:
foreach (var item in list)
{
DoSomething(item);
}
}
I tried to use same psuedo-code style as OP. The logic replicates the original, but in condensed form and with advantage of easily adding lists to the master list array (as suggested by #Benjamin).
It is assumed that DoSomething is a single function applied to the first list which has Count > 1. If you wanted to apply a custom function per list, you could create another array for holding the function to call for the corresponding list.
listArray = new[] {list1, list2, list3, list4}; // you can add more as needed to this master array
for (int i = 0; i < listArray.Count; i++)
{
if (listArray[i].Count > 1)
{
for (int i = 0; i < listArray.Count; i++)
{
DoSomething(listArray[i]);
}
break; // http://msdn.microsoft.com/en-us/library/adbctzc4.aspx
}
}
You can check first which list you want to use:
var lists = new[]{ list1, list2, list3, list4 };
var list = lists.FirstOrDefault(l => l.Count > 1);
if(list != null)
{
list.ForEach(l => DoSomething(list));
}
Seems like you need the first list for which Count > 1.
Also, inside the for-loop, you're saying DoSomething(list1);. I think it should be DoSomething(list1[i]);
var lists = new [] { list1, list2, list3, list4 };
var target = lists.FirstOrDefault(l => l.Count() > 1);
if (target != null) {
for (int i = 0; i < target.Count; i++)
{
DoSomething(target[i]);
}
} else {
//...
}

Need to add each number 0-x to end of line?

I'm making an app and I'm almost done. I just need to know how I can streamread a txt list and foreach line, add numbers 0-x (x will be the number the user puts in the textbox) and add it to a list. So basically, it would be like this
You import a list with 'dog' on one line, 'cat' on another, and 'fish' on the third. You type '5' into the textbox. the app puts all this into a list:
dog1
dog2
dog3
dog4
dog5
cat1
cat2
cat3
cat4
cat5
fish1
fish2
fish3
fish4
fish5
thanks!
The code below should work for you. I assume you can acquire the count value on your own.
var animals = File.ReadAllLines("yourFile.txt"); //new[] {"dog", "cat", "fish"};
var count = 5;
var merged =
from a in animals
from n in Enumerable.Range(1, count)
select a + n;
foreach (var m in merged)
Console.WriteLine(m); //act on each however you want
You can read a text file with File.ReadAllLines. This gives you an array you can iterate over with foreach.
In this foreach loop you can perform another loop from 1 to the number the user entered. int.Parse comes in handy for converting the string the user entered into a number C# can do something with. For the actual iteration you can use a for loop.
You can then add each item to a list.
There is a good example for reading each line in a filestream here: http://msdn.microsoft.com/en-us/library/e4y2dch9.aspx
private List<string> GetThings(string fileName, int count)
{
string[] lines = File.ReadAllLines(fileName);
List<string> result = new List<string>();
foreach (string item in lines)
{
for (int i = 1; i <= count; i++)
result.Add(item + i.ToString());
}
return result;
}
string[] inputList = File.ReadAllLines("yourFile.txt");
List<String> listOfThings = new List<String>();
foreach (string i in inputList)
{
for (int k = 0; k < 5; k++)
{
listOfThings.Add(i + " " + k.ToString());
}
}
then after that, you can print out the list like this:
foreach (string outp in listOfThings)
{
Console.WriteLine(outp);
}
output:
some value 0
some value 1
some value 2
some value 3
some value 4
some other value 0
some other value 1
some other value 2
some other value 3
some other value 4

Looping through x number of arrays

How do I loop through x number of arrays and visit all combinations of all cells in all of the arrays? The problem here is there can be x number of arrays of some items inside. For instance,
List<List<string>> _arrays = GetArrayInformation();
I want to compare all the string inside each array with all the other arrays and the strings inside of each array. Do I use while like
while(i < _arrays.Count)
Thanks for your answer. The answer seems simple but when you think about it is kind of tricky and hard.
Update:
Thanks for your answers. I can do this with a 3 arrays like
for(int i = 0; i < _arrays[0].Count; i++) {
for(int l = 0; l < _arrays[1].Count; l++) {
for(int m = 0; m < _arrays[2].Count; m++) {
string _hello = _arrays[0][i] + "|" + _arrays[1][l] + "|" + _arrays[2][m];
}
}
}
Because I have dynamic number of arrays, it gets tricky.
foreach(var array in _arrays)
{
foreach(var s in array)
{
foreach(var otherArray in _arrays)
{
if(otherArray == array) continue;
if(otherArray.Contains(s)) {} // not sure what you want to do
}
}
}
this will loop through every single string seeing if it is in any other array.... it's the straightforward approach, but not very efficient and will duplicate work.
There is no enough information is here
If you need to find elements that exists in few array You will use something like this:
var multipleWords = _arrays
.SelectMany(items => items.Distinct())
.GroupBy(item => item)
.Select(group => new {Item = group.Key, Count = group.Count()})
.Where(item => item.Count > 1)
.Select(item => item.Item)
.ToArray();
multipleWords will contain each word from the all these arrays that exists in two or more arrays
You could use a recursive search function.
public Search<T>(object o, string comparestring)
{
if(o is List<string>)
{
//Compare to your string
}
else
{
//Call this search function with the type of the object in the list.
//Will iterate through all of your strings recursively.
Type t = o.GetType().GetGenericArguments()[0];
foreach( object osub in (T)o)
Search<t>( ((t)o),comparestring)
}
}

Exception during iteration on collection and remove items from that collection [duplicate]

This question already has answers here:
What is the best way to modify a list in a 'foreach' loop?
(11 answers)
Closed 9 years ago.
I remove item from ArrayList in foreach loop and get follwing exception.
Collection was modified; enumeration operation may not execute.
How can I remove items in foreach,
EDIT: There might be one item to remove or two or all.
Following is my code:
/*
* Need to remove all items from 'attachementsFielPath' which does not exist in names array.
*/
try
{
string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names.
string[] names = attachmentFileNames.Split(new char[] { ';' });
int index = 0;
// attachmentsFilePath is ArrayList holding full path of fiels user selected at any time.
foreach (var fullFilePath in attachmentsFilePath)
{
bool isNeedToRemove = true;
// Extract filename from full path.
string fileName = fullFilePath.ToString().Substring(fullFilePath.ToString().LastIndexOf('\\') + 1);
for (int i = 0; i < names.Length; i++)
{
// If filename found in array then no need to check remaining items.
if (fileName.Equals(names[i].Trim()))
{
isNeedToRemove = false;
break;
}
}
// If file not found in names array, remove it.
if (isNeedToRemove)
{
attachmentsFilePath.RemoveAt(index);
isNeedToRemove = true;
}
index++;
}
}
catch (Exception ex)
{
throw ex;
}
EDIT: Can you also advice on code. Do I need to break it into small methods and exception handling etc.
Invalid argument exception On creating generic list from ArrayList
foreach (var fullFilePath in new List<string>(attachmentsFilePath))
{
When I use List<ArrayList> the exception is
Argument '1': cannot convert from 'System.Collections.ArrayList' to 'int'
attachmentsFilePath is declared like this
ArrayList attachmentsFilePath = new ArrayList();
But when I declared it like this, problem solved
List<ArrayList> attachmentsFilePath = new List<ArrayList>();
Another way of doing it, start from the end and delete the ones you want:
List<int> numbers = new int[] { 1, 2, 3, 4, 5, 6 }.ToList();
for (int i = numbers.Count - 1; i >= 0; i--)
{
numbers.RemoveAt(i);
}
You can't remove an item from a collection while iterating over it.
You can find the index of the item that needs to be removed and remove it after iteration has finished.
int indexToRemove = 0;
// Iteration start
if (fileName.Equals(names[i].Trim()))
{
indexToRemove = i;
break;
}
// End of iteration
attachmentsFilePath.RemoveAt(indexToRemove);
If, however, you need to remove more than one item, iterate over a copy of the list:
foreach(string fullFilePath in new List<string>(attachmentsFilePath))
{
// check and remove from _original_ list
}
You can iterate over a copy of the collection:
foreach(var fullFilePath in new ArrayList(attachmentsFilePath))
{
// do stuff
}
List<string> names = new List<string>() { "Jon", "Eric", "Me", "AnotherOne" };
List<string> list = new List<string>() { "Person1", "Paerson2","Eric"};
list.RemoveAll(x => !names.Any(y => y == x));
list.ForEach(Console.WriteLine);
while enumerating (or using foreach) you cannot modify that collection. If you really want to remove items, then you can mark them and later remove them from list using its Remove method
do the following:
foreach (var fullFilePath in new List(attachmentsFilePath))
{
this way you create a copy of the original list to iterate through
You could loop over the collection to see which items need to be delete and store those indexes in a separate collection. Finally you would need to loop over the indexes to be deleted in reverse order and remove each from the original collection.
list<int> itemsToDelete
for(int i = 0; i < items.Count; i++)
{
if(shouldBeDeleted(items[i]))
{
itemsToDelete.Add(i);
}
}
foreach(int index in itemsToDelete.Reverse())
{
items.RemoveAt(i);
}

Categories