Re:What's wrong with this code, really?
by altrego99 on Friday September 16, 2011 @09:11AM
(#3004286)
Isn't clear() itself a "while (not empty) delete element"?
[ Parent
| on HN ]
Re:What's wrong with this code, really?
by StrawberryFrog on Friday September 16, 2011 @11:00AM
(#3004689)
A while loop isn't in itself an iterator. There's a difference between
while(collection.Count > 0)
{
collection.Delete(0);
}
.. which is a working implementation of clear, though probably superfluous.and...
int i = 0;
while(i < collection.Count)
{
collection.Delete(i);
i++;
}
which does try to walk along the list and is broken.Also, .Net has a specific exception to stop you modifying a collection while an enumerator is walking along it. Google for "Collection was modified; enumeration operation may not execute"
Re:What's wrong with this code, really?
by dkersten on Friday September 16, 2011 @09:18AM
(#3004301)
Maybe, maybe not - depends on the container.
But thats not the point - while (not empty) is not iterating over the loop, so there is no danger of corrupting the iterator.
Very unlikely. There class implementation can almost certainly do it with an O-1 operation, like "self._internal_count = 0".
Re:What's wrong with this code, really?
by jcromartie on Friday September 16, 2011 @10:09AM
(#3004483)
or self._tabs = new List();