-1

I am trying to remove invalid elements of an JArray while iterating over it. Doing this in a foreach loop obviously doesn't work because it doesn't expect the array to get shorter. So I decided to do use a for loop. I am doing it like this:

// data is my JArray, its elements are JObjects
for (int i = 0; i < data.Count; i++)
{
    if (!data[i].HasValues) { // checking if element is empty
        data[i].Remove();
        i--;
    }
}

When I tested this it worked correctly. But I am not sure if it is safe or if I am just lucky. For example, I am not sure if the order of the elements stays the same after removing one element. Or are there any other reasons why this could not work?

I guess the best alternative, if this was incorrect, would be to copy all valid elements to a new array. But that would cause a lot of overhead.

arne.z
  • 2,720
  • 2
  • 20
  • 41
  • 3
    When deleting from an array you have to work from end of list towards beginning : for(int i = data.count - 1; i >= 0; i--) – jdweng Mar 12 '16 at 17:23
  • @jdweng: Thanks for your comment. Yes, starting from the end is better than my solution – arne.z Mar 12 '16 at 18:27
  • @MarkSchultheiss: Why would that be a duplicate? I know how to remove an element: `data[i].Remove()`. The question is about deleting them in a for loop. – arne.z Mar 12 '16 at 18:28
  • @洋葱头: Creating a new array won't cause *a lot* of overhead. An array is nothing but a list of references to the original objects. If both lists refer to the same object, it doesn't mean you have 2 instances of the same object... – code4life Mar 12 '16 at 18:31
  • @code4life: Okay, thanks for the clarification. I'd still prefer just working with one array, if copying is not necessary. – arne.z Mar 12 '16 at 18:35
  • @洋葱头 - did you check all the answers there? What you ask for IS there. – Mark Schultheiss Mar 12 '16 at 18:42
  • @MarkSchultheiss: Yes I did and I didn't find an answer there. Can I just assume that JArrays in C# behave the same way as Arrays in JavaScript? And that `data[i].Remove()` behaves the same as `data.splice(i, 1);`? I found an answer there that suggests to start iterating from the end of the array, but that's not the whole answer to my problem. – arne.z Mar 12 '16 at 19:05

1 Answers1

1

You can do this manually in a better way; when you find a "faulty" element you want to remove, you just shift all other elements to the left thus overriding the one you don't want. But I think there's no need for you to do that. (if you're adamant in this request, comment and I'll provide you with the code for doing so).

Remove() method does that for you. Most methods for element insertion or removal from collections do work this way: they insert/delete the element at a desired position and shift other elements to the right/left respectively.

Vucko
  • 7,000
  • 2
  • 21
  • 42
  • Okay, so I can be sure that the order of the element does not change? If I don't use the `i--`, won't I skip the element that got shifted into the position of the deleted element? – arne.z Mar 12 '16 at 18:23
  • you're probably right about `i--` yeah. About being sure - google a little bit more the Remove method or input some test examples like 1,2,3,4,5,EMPTY,6,7,8,EMPTY,9,10, apply the method and then just output the array to see if it changed the order. It should work correctly. – Vucko Mar 12 '16 at 18:26