Search code examples
javaoopintellij-ideaarraylistreturn-type

My return type should be right but Intelli-J says it is wrong? ArrayList<ArrayList<Card>> needed, boolean found


I am writing (essentially) an N choose R method that should return a list of possible combinations. Intelli-J is telling me that my ArrayList which I am adding ArrayList to says that its returning a boolean type in the first if statement instead. And in the next few statements I am doing the same thing but it doesn't show that I have any errors there. Here is my code (my recursion is probably wrong right now but I can't test my methof until this error is fixed):

private ArrayList<ArrayList<Card>> choose(ArrayList<Card> from, int howMany)
{
    ArrayList<ArrayList<Card>> toReturn = new ArrayList<ArrayList<Card>>();
    if (from.size() == howMany)
    {
        return toReturn.add(from);
    }
    else if (howMany == 1)
    {
        for (int i = 0; i < from.size() -1; i++)
        {
            ArrayList<Card> oneCardList = new ArrayList<Card>();
            Card card = from.get(i);
            oneCardList.add(card);
            toReturn.add(oneCardList);
        }
        return toReturn;
    }
    for (Card card : from) {
        Card first = card;
        ArrayList<Card> theRest = new ArrayList<Card>(from.subList(1, from.size() - 1));
        toReturn.add(theRest);
        ArrayList<ArrayList<Card>> chooseRest = choose(theRest, howMany);
    }
    return toReturn;
}

Solution

  • You are returning toReturn.add(from); that returns a boolean, not a List.
    I guess you should do something like this:

    toReturn.add(from);
    return toReturn;