0

I have a class Cell and a class Neighbour extending Cell. But I get an error when I try to pass an ArrayList<Neighbour> to a function expecting an ArrayList<Cell>. What have I missed?

class Cell {
    PVector pos;

    Cell(PVector pPos) {
        pos = pPos.get();
    }
}

class Neighbour extends Cell {
    int borders = 0;

    Neighbour(PVector pPos) {
        super(pPos);
    }
}

private int inSet(PVector pPos, ArrayList<Cell> set) {
    [...]

    return -1;
}

[...]

ArrayList<Neighbour> neighbours = new ArrayList<Neighbour>();
PVector pPos = new PVector(0, 0);

[...]

inSet(pPos, neighbours);

The last line throws the error `The method iniSet(PVector, ArrayList) is not applicable for the arguments (PVector, ArrayList);

Thanks for your help!

1

2 Answers 2

3

that is because

List<A> != List<B> ... even if B extends A.

What you need to do is modify the function to the following

private int inSet(PVector pPos, ArrayList<? extends Cell> set) {
    [...]
    return -1;
}

Hope that helps.

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

Comments

2

Try with:

private int inSet(PVector pPos, List<? extends Cell> set)

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.