3

My problem is this: I have an iterator class which is supposed to iterate through elements in a given data structure, <E> let's say, but what I have managed to accomplish is that when I pass in the data structure it will iterate the data structure itself.

ie. DynamicIterator it = new DynamicIterator(da);
say da is an array the output will be [1,2,3,4,5,6] instead of 1,2,3,4,5,6

My issue is, more than anything, understanding the generally accepted practice for dealing with this more than the issue itself.

edit for code:

public class X<E>
{
    private final E[] rray;
    private int currentIndex = 0;

    public X(E... a) 
    {
        //if the incoming array is null, don't start
        if(a == null)
        {
            System.out.println("Array is null");
            System.exit(1);
        }
        //set the temp array (rray) to the incoming array (a)
        this.rray = a;
    }

    //hasNext element?
    public boolean hasNext()
    {
        return rray.length > currentIndex;
    }

    //next element (depends on hasNext())
    public E next()
    {
        if (!hasNext())
        {
            System.out.println("Element doesn't exist, done");
            System.exit(1);
        }
        return rray[currentIndex++];
    }

    //return array
    public E[] access()
    {
        return rray;
    }
}
10
  • You can do this using reflection. Follow this tutorial to get each field and value from a class: tutorials.jenkov.com/java-reflection/fields.html. Commented Jul 9, 2013 at 15:51
  • The problem here is that we don't know anything about the underlying data structure, only that you can iterate it. Commented Jul 9, 2013 at 15:59
  • This doesn't make sense. Can we see some code? Commented Jul 9, 2013 at 16:02
  • Oh, looks like I got the wrong problem. Your question title confused me. Could you provide an example of your input and output in code? Commented Jul 9, 2013 at 16:02
  • sure thing code is edited in above, i realize the variable is labelled rray (the example I'm working with is a custom array) Commented Jul 9, 2013 at 16:06

1 Answer 1

1

You won't be able to do this with a completely generic parameter <E> - how would you iterate through a Throwable, for example? What your class X does at the moment is accept any number of objects in its constructor, and then simply returns each of those objects in turn.

If you restricted the bounds of the objects passed in to implement e.g. Iterable, then you can actually start to "look inside" them and return their contents:

public class X<E> {
    private final Iterator<E> it;

    public X(Iterable<E> a) {
        it = a.iterator();
    }

    public boolean hasNext() {
        return it.hasNext();
    }

    public E next() {
        return it.next();
    }
}

Although this doesn't really accomplish anything different to just using a.iterator() directly instead of an instance of X...

Sign up to request clarification or add additional context in comments.

2 Comments

True enough, honestly the problem I was given confused me for this very reason. I really appreciate your insight though, Iterable is completely new to me so your response is totally awesome and what I was expecting; I just didn't realize the solution would be so simple.
As soon as I have enough rep, I'll come back and Vote up you. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.