1

I'm trying to build a extension of ArrayList and build a custom Iterator for it, all in generic types, and I'm getting errors before compilation which I do not understand.

public class GenerateurBiGramme <T> extends ArrayList <T> {
    public Iterator<Pair<T,T>> iterator(int delta){
        return new BiGramme<>(delta);
    }
    public class BiGramme <T> implements Iterator<Pair<T, T>>{
        int premier = 0;
        int dernier = 1;
        int delta;
        public Pair<T, T> next() {
            Pair<T,T> temp = new Pair<T,T>(get(this.debut),get(this.dernier));
            return temp;
        }

My IDE is telling me that "Pair(T,T) in Pair cannot be applied to (T,T).

I don't understand what it means by that.

Thank you for your help!

EDIT: here is my "Pair" class:

public class Pair<T, U> {
    public T premiere;
    public U deuxieme;

    public Pair( T premiere, U deuxieme ) {
        this.premiere = premiere;
        this.deuxieme = deuxieme;
    }
}

Basically, I'm going to feed a table into the generator, and I intend to use "premier" and "dernier" to create pairs at a set distance from one another...

3
  • Is that org.apache.commons.lang3.tuple.Pair or some other Pair class? If it's your own class, can you show us the code please? Commented Jun 9, 2018 at 2:21
  • Could you post your whole class? There's so much to guess about BiGramme, this.debut, this.dernier... Commented Jun 9, 2018 at 2:28
  • I edited the post with the requested information. Commented Jun 9, 2018 at 3:05

1 Answer 1

2

Your problem is that in your declaration public class BiGramme <T>, the T hides the T that you declared in public class GenerateurBiGramme <T>. You now have two different type parameters to GenerateurBiGramme.BiGramme, but unfortunately, they're both called T.

You don't need the T in the declaration of BiGramme, because it's an inner class, so it already has access to the type parameters of the outer class, GenerateurBiGramme. Just change the declaration to public class BiGramme, without the T. Also remove the <> from the line return new BiGramme(delta);. This worked for me.

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

Comments

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.