Превратить метод в рекурсивный - PullRequest
0 голосов
/ 21 марта 2019

Я хочу, чтобы в схеме Понци отображалось общее количество пользователей, которые у меня есть ниже.

Это жестко закодированный метод, который работает, и я хочу сделать его рекурсивным

public int getAllChildrenFromDown(List<Credential> children) {
    if (!(children.size() == 0)) {
        for (Credential child : children) {
            count = count + 1;
            List<Credential> grandChildren = credentialRepository.findByParentId(child.getParentId());
            if (!(grandChildren.size() == 0)) {
                for (Credential grandChild : grandChildren) {
                    count = count + 1;
                    List<Credential> greatGrandChildren = credentialRepository.findByParentId(grandChild.getParentId());
                    if (!(greatGrandChildren.size() == 0)) {
                        for (Credential greatGrandChild : greatGrandChildren) {
                            count = count + 1;
                            List <Credential> greatGreatGrandChildren =credentialRepository.findByParentId(greatGrandChild.getParentId());
                            if(!(greatGreatGrandChildren.size()==0)) {
                                for (Credential greatGreatGr: greatGreatGrandChildren) {
                                    count=count +1;
                                }
                            }
                        }
                    }
                }

            }
        }
    }
    return count;
}

Ниже то, что я пытался, но я получаю бесконечный цикл

public int getAllChildrenFromDown(List<Credential> children) {

    if (!(children.size() == 0)) {
        for (Credential child : children) {
            count = count + 1;
            List<Credential> grandChildren = credentialRepository.findByParentId(child.getParentId());
            getAllChildrenFromDown(grandChildren);

        }
    }
    return count;
}

Есть идеи?

1 Ответ

0 голосов
/ 21 марта 2019

Как заметил @doelleri выше, решение credentialRepository.findByParentId (child.getId ());

вместо credentialRepository.findByParentId (child.getParentId ());

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...