Removing Items in a List While Iterating Through It with For Each Loop

前端 未结 7 2268
小鲜肉
小鲜肉 2021-01-17 09:56

I have a list named NeededList I need to check each item in this list to see if it exists in my database. If it does exist in the database I need to remove it

7条回答
  •  佛祖请我去吃肉
    2021-01-17 10:13

    The contents of an array (or anything else you can fast enumerate with For Each can not be modified with a For Each loop. You need to use a simple For loop and iterate through every index.

    Hint: Because you'll be deleting indexes, I suggest starting at the last index and work your way toward the first index so you don't skip over one every time you delete one.

提交回复
热议问题