-3

Hey so I'm trying to execute this bit of code, however it is going out of bounds I would assume due to it trying to execute the first loop at the index that was removed. Does anyone know a way I can execute this code without it going out of bounds?

  for (int i = myList1.Count - 1; i >= 0; i--)
    {
        for (int j = 0; j < myList2.Count - 1; j++)
        {
            if (myList2[j] != myList1[i])
            {
                myList1.RemoveAt(i);
            }
        }
    }
Lewis
  • 566
  • 4
  • 21
  • 1
    Iterate through a copy and alter the original. Remember that if you remove item 0, *all items after it* will move by 1. What was item 1 will be the new item 0. Counting down rather than up is an easy way to work around that issue. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '18 at 19:18
  • Make a copy of myList1 and remove from that. Then use the copy for whatever you need to do after that. In general you want to avoid modifying the size of something you're iterating through. – emsimpson92 May 24 '18 at 19:20
  • Yeah that's why I've done the backwards loop, what do you mean by iterating through a copy and altering the original? – Lewis May 24 '18 at 19:20
  • 1
    Why are you iterating through mylist1 and mylist2 but using the indices to look at the variables in intVariable1 and intVariable2?? Shouldn't you be looking at mylist2[j] and mylist1[i]? – MikeS May 24 '18 at 19:20
  • Ahh I see, I'll give it a go, thanks. – Lewis May 24 '18 at 19:21
  • This sort of code gives me a headache. Consider using LINQ operators like `Except` and `Contains` if possible, or even `HashSet` if that's a more appropriate structure (`Intersect` is nice, you know). In some cases this means making redundant copies, but optimizing should be the second concern, not the first one. – Jeroen Mostert May 24 '18 at 19:23
  • Your code as shown assuming `intVariable1` and `intVariable2` were meant to be `myList1` and `myList2` seems to say remove every entry in `myList1` that doesn't have an equal entry _in the same position_ in `myList2` - is that really what you meant? – NetMage May 24 '18 at 19:26
  • Possible duplicate of [How to remove elements from a generic list while iterating over it?](https://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it) – ElasticCode May 25 '18 at 00:39

1 Answers1

0

Obligitory Linq Answer:

myList1 = myList1.Where(i => !myList2.Contains(i)).ToList();

Basically, instead of looping through them both on your own, you use Linq to do it for you. You set myList1 to be the items matching the where clause of [myList2 does not contain item X].

Kevin
  • 2,133
  • 1
  • 9
  • 21
  • That doesn't seem to match the code to me? The code seems to remove every `myList1` entry that isn't in the same position in `myList2` assuming that is what was meant instead of `intVariable1` and `intVariable2`! – NetMage May 24 '18 at 19:25
  • Not sure. I'm *assuming* that the OP meant to be comparing the elements of the two lists, instead of a separate set of arrays called intVariable1 and intVariable2 (otherwise, why loop through myList1 instead of intVariable1?). If it turns out I'm wrong, I'd still suggest solving it with Linq (though the code will be a bit different.) – Kevin May 24 '18 at 19:29