Link to home
Start Free TrialLog in
Avatar of lblinc
lblincFlag for United States of America

asked on

convert to C# - IEnumerator vs. Java's Iterator

Anyone, the right answer to this is worth a lot to me today, so..  anyone who knows a little about Java..  and a lot about C#..  should be able to answer this..

Are these exactly the same?   Or..  do they accomplish the exact same thing?   Please confirm.      (in below examples the price variable is passed in as a parameter.. not shown, but  not important to answer the question)

C#:
                  for (IEnumerator i = buyList.GetEnumerator(); i.MoveNext(); )
                  {
                        BuyPrice buy_from_list = (BuyPrice) i.Current;
                        if (buy_from_list.price > price)
                        {
                              buyList.Remove(i);        // remove old
                              bFixed = true;
                        }
                  }
                  
Java:            
                  for (Iterator i = buyList.iterator(); i.hasNext(); )
                  {
                        BuyPrice buy_from_list = (BuyPrice) i.next();
                        if (buy_from_list.price > price)
                        {
                              i.remove();           // remove old
                              bFixed = true;
                        }
                  }

OR.. in C#..  using IEnumerator..   is there no need to use the remove at all?    as follows:

                                                for (IEnumerator i = buyList.GetEnumerator(); i.MoveNext(); )
                  {
                        BuyPrice buy_from_list = (BuyPrice) i.Current;
                        if (buy_from_list.price > price)
                        {
                        // i.remove();           //  no need to remove here ..   thus commented out.
                              bFixed = true;
                        }
                  }



Note:  

The JLCA (Java Language Converter) only gives the following response after conversion:
----------------------------------------------
JLCA Explaination:
 
In the Java language, this method      i.remove();    removes the last element obtained by the next method.

In the .NET Framework, there is no direct equivalent.

To correct this error

Use a class that implements the System.Collections.IEnumerator interface.

------------------------------------------------

...  and that really doesn't help much to write the exact equivalence.

Thanks in advance.



Java Supporting Definitions:

 boolean hasNext();

 /**
     * Returns <tt>true</tt> if the iteration has more elements. (In other
     * words, returns <tt>true</tt> if <tt>next</tt> would return an element
     * rather than throwing an exception.)
     *
     * @return <tt>true</tt> if the iterator has more elements.
     */
   
 Object next();

  /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration.
     * @exception NoSuchElementException iteration has no more elements.
     */
 

 void remove();

/**
     *
     * Removes from the underlying collection the last element returned by the
     * iterator (optional operation).  This method can be called only once per
     * call to <tt>next</tt>.  The behavior of an iterator is unspecified if
     * the underlying collection is modified while the iteration is in
     * progress in any way other than by calling this method.
     *
     * @exception UnsupportedOperationException if the <tt>remove</tt>
     *              operation is not supported by this Iterator.
     
     * @exception IllegalStateException if the <tt>next</tt> method has not
     *              yet been called, or the <tt>remove</tt> method has already
     *              been called after the last call to the <tt>next</tt>
     *              method.
     */
   }
ASKER CERTIFIED SOLUTION
Avatar of dorothy2
dorothy2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lblinc

ASKER

dorothy, thanks for confirming this.