0

C#'s IEnumerator doesn't have a next() and remove() how do I solve this? (hitScans is an ArrayList)

// Iterator it = enemyWaves.iterator();
   IEnumerator it = hitScans.GetEnumerator();
         while (it.MoveNext())
            {
                if ((dist = ((ew = (EnemyWave)it.next()).distanceTraveled += ew.bulletVelocity)
                - myLocation.distance(ew.fireLocation)) > 50)
                {
                    it.remove();
                    continue;
                }

and

// Iterator i = hitScans.iterator();
   IEnumerator i = hitScans.GetEnumerator();
                while (i.MoveNext())
                {
                    double[] scan = (double[])i.next();
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Jacklyn
  • 19
  • 6

2 Answers2

1

Do you realy need use Iterator/Enumerator??

If you just need to remove elements from ArrayList while iterating it (whitch is commonly discussed issue), I would propose something like this:

for (int i=hitScans.Count-1;i>=0;i--){
   if (/*your difficult condition*/){
      hitScans.RemoveAt(i);   
   }
}

and for the second part: foreach - isn't it solution?

MisterMe
  • 149
  • 10
0

Regarding 'remove', there is no direct .NET equivalent since IEnumerable is not intended to be modifiable. There are some work-arounds: Want to remove an item from a IEnumerable<T> collection

Regarding 'next', note that even though each of 'hasNext' and 'next' are not equivalent to C# 'MoveNext' and 'Current', when used together they produce the same effect.

e.g.,

Iterator it = enemyWaves.iterator();
while (it.hasNext()) {
    Object o = it.next();
}

will typically convert to the following C# code:

IEnumerator it = enemyWaves.GetEnumerator();    
while (it.MoveNext())
{
    object o = it.Current;
}
Community
  • 1
  • 1
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28