Avatar of lblinc
lblinc
Flag 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.
     */
   }
C#

Avatar of undefined
Last Comment
lblinc

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dorothy2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
lblinc

ASKER
dorothy, thanks for confirming this.    
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck