0

I found (here)a sort of implementation of an Iterator in c# that mimics Java's Iterator methods but I'm not quite sure how to use it in my case or if there's an easier option to rewrite this method c#'s way. Original java method:

public RWIterator<T> search (T e){

    RWIterator<T> it =null;
    RWIterator<T> current = iterator();
    RWIterator<T> prev = iterator();
    boolean found=false;
    while(! found && current.hasNext()){
        T el=current.next();
        if (el.equals(e)) found = true;
        else prev.next();
    }
    if (found) {
        //Iterator<T> tmp = prev;
        //System.out.println("searched value is="+tmp.next());
        it = prev;
    }
    return it;
}

Where RWIterator extends java Iterator, I can't understand the use of prev .I'm stuck at this point:

public RWIterator<T> Search(T e)
{

    RWIterator<T> it = null;
    RWIterator<T> current = Iterator();
    RWIterator<T> prev = Iterator();
    bool found = false;
    while (!found && current.GetEnumerator().MoveNext())
    {
        T el = current.GetEnumerator().Current;
        if (el.Equals(e)) found = true;
        else
        {
           //what should happen with prev.GetEnumerator().Current ?
        }
    }
    if (found)
    {
        //Iterator<T> tmp = prev;
        //System.out.println("searched value is="+tmp.next());
        it = prev;
    }
    return it;
}
Community
  • 1
  • 1
David
  • 127
  • 2
  • 9
  • 3
    What are you really trying to achieve? This doesn't look like particularly clean Java code either... if you could explain the bigger picture, it would be easier to help you. – Jon Skeet Apr 27 '15 at 12:49
  • @Jon :It's a sort of framework uses the basic Java interfaces from the package java.util: Iterator and Iterable. The class Storage implements Iterable interface (from which this methods are) Also there is an abstraction IReference that corresponds to the logical denition of a reference.Considering that iterators are also references since they correspond to the reference def. Because of this, the class RWIterator extends the both interfaces Iterator from java.util package, and IReference. The main point of project is to use Decorator an proxy pattern to obtain specialized collections. – David Apr 27 '15 at 13:03
  • the method Iterator() -obtains an reading iterator over the elements of the storage . – David Apr 27 '15 at 13:03
  • 1
    I can't help but feel you'd almost certainly get to a cleaner solution if you *didn't* try to mix Java and .NET. It's not clear what `RWIterator` is (What does the RW stand for?) or what `search` is meant to do.. while you probably *can* port the code this way, it doesn't sound like it's a good idea. – Jon Skeet Apr 27 '15 at 13:05
  • 1
    (Aside from anything else, the current method ends up reading over storage twice, presumably. That doesn't sound like a great idea...) – Jon Skeet Apr 27 '15 at 13:06
  • 1
    Also you should only call `GetEnumerator()` once. Typically each call to `GetEnumerator()` returns a new enumerator pointing to the beginning of the enumerable. – Kyle Apr 27 '15 at 13:08

3 Answers3

2

You can get a reference to the Enumerator, using the GetEnumerator method, then you can use the MoveNext method to move on, and use the Current property to access your elements:

var enumerator = getInt().GetEnumerator();
while(enumerator.MoveNext())
{
    int n = enumerator.Current;
    Console.WriteLine(n);
}
MRebai
  • 5,344
  • 3
  • 33
  • 52
1

Ok Buddy, it seams that what your search Method is trying to achieve is to run throughout an iterator until it finds a match. Then it will return an iterator starting from this match. Here is an alternative to your method that achieves what I just described:

public RWIterator<T> Search(T e)
{
    RWIterator<T> myIterator = Iterator();
    bool found = false;
    foreach (T element in myIterator)
    {
        if (found)
            yield return element;
        else if (element.Equals(e))
        {
            found = true;
            yield return element;
        }
    }
}
Rodrigo López
  • 4,039
  • 1
  • 19
  • 26
0

C# has the foreach-loop to access IEnumerables. Every object, which has a GetEnumerator-method can be accessed using foreach.

I think you shouldn't try to translate Java to C# directly, since there are several differences in the underlying concepts. Where you have Iterators in Java, you have IEnumerable-implementations in C#. The iterating then is done by using foreach.

public RWIterator<T> Search(T e)
{

    RWIterator<T> it = null;
    RWIterator<T> current = Iterator();
    RWIterator<T> prev = Iterator();
    bool found = false;
    // while (!found && current.GetEnumerator().MoveNext())
    foreach(T el in current)
    {
        // T el = current.GetEnumerator().Current;
        if (el.Equals(e)) found = true;
        else
        {
           //what should happen with prev.GetEnumerator().Current ?
        }
    }
    if (found)
    {
        //Iterator<T> tmp = prev;
        //System.out.println("searched value is="+tmp.next());
        it = prev;
    }
    return it;
}
Patrik
  • 1,355
  • 12
  • 22
  • I believe that `Iterator` is equivalent to `IEnumerator`, not `IEnumerable` (which is equivalent to `Iterable`) – juharr Apr 27 '15 at 12:58
  • @juharr Sure, you're right. But this one wasn't the best answer anyway, since the problem was a bit more in depth. – Patrik Apr 27 '15 at 14:51