1

I am trying to write an itertor for a type of ArrayList, where Number is a class.

I can almost do it, but I get errors:

This is the class definition

public class History implements Iterable<ArrayList<Number>> {

This is the definition of the type I wish to iterate over:

private ArrayList<Number> spins = new ArrayList<Number>();

... and here are the iterator functions:

   @Override
    public Iterator<ArrayList<Number>> iterator() {
        Iterator<ArrayList<Number>> it = new Iterator<ArrayList<Number>>() {

            private int currentIndex = 0;

            @Override
            public boolean hasNext() {
                return currentIndex < gethistorySize() && spins.get(currentIndex) != null;
            }

            @Override
            public ArrayList<Number> next() {
                return spins.get(currentIndex++); // ** ERROR **
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
        return it;
    }

I get an error at the lines marked ** ERROR **

The error I get is:

Multiple markers at this line - The type of expression must be an array type but it resolved to ArrayLisy - Type mismatch: Cannot convert from Number to ArrayList

I am not quite sure what to return here.

Can anybody help?

Thx

3
  • Multiple markers at this line - The type of expression must be an array type but it resolved to ArrayLisy<Number> Type mismatch: Cannot conver from Number to ArrayList<Number> Commented Nov 27, 2016 at 10:32
  • Could you add error message so people with same problem would be able to find your question and possible answers? Commented Nov 27, 2016 at 10:32
  • Done above, sorry - and, just to note, it is not a run time error, but a "screen error" so cannot complie Commented Nov 27, 2016 at 10:35

2 Answers 2

1

In order for spins.get(currentIndex++) to return an ArrayList<Number>, the type of spins would have to be List<ArrayList<Number>>, not ArrayList<Number>.

If the type of spins is correct, perhaps what you need is to implement an Iterator<Number>, not an Iterator<ArrayList<Number>> (and History should implement Iterable<Number>).

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

1 Comment

Thnak you - I will try these suggestions
0

Change

        @Override
        public ArrayList<Number> next() {
            return spins.get(currentIndex++); // ** ERROR **
        }

To

        @Override
        public Number next() {
            return spins.get(currentIndex++); // ** ERROR **
        }

Return type is wrong hence the compile error. When you call next() method, you want to iterate each Number object within An ArrayList<Number>.

3 Comments

Thansk - just for info - that removs the original error, but gives a (similar) error on the changed line
@Graham link try some of this example
Many thanks all - changing the type to <Number> respolved this.

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.