Thursday, February 25, 2016

CS 2420 - Assignment 4 - Part 4

Since last time I have been working on implementing my own Enumerator in my List class. I started to work on it Tuesday night while in class, but I was actually stuck on this assignment until earlier today when I figured it out.

Here is the finished Enumerator class:

private class myEnumerator : IEnumerator
{
    private MyList myList;
    private int index;

    public myEnumerator(MyList myList)
    {
        this.myList = myList;
        index = -1;
    }

    public bool MoveNext()
    {
        index++;
        return (index < myList.Count);
    }

    public T Current
    {
        get
        {
            return myList[index];
        }
    }

    object IEnumerator.Current
    {
        get
        {
            return Current;
        }
    }

    // Not Needed
    public void Dispose()
    {
        //Not Needed
    }

    public void Reset()
    {
        index = -1;
    }
}

This new class is embedded in the List class, and it just spits out the next item in the list. The cool thing about this new Enumerator is that it will stop as soon as it reached the Count of the items in the list instead of the length of the array. This way if there are any empty slots in the array, they won't get printed in the list.

That's all for now, thanks for reading!

No comments:

Post a Comment