troubleshooting Question

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

Avatar of lblinc
lblincFlag for United States of America asked on
C#
2 Comments1 Solution5109 ViewsLast Modified:
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
dorothy2

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros