Java 计算有多少“元素”出现在双链接列表中

Java 计算有多少“元素”出现在双链接列表中,java,doubly-linked-list,find-occurrences,Java,Doubly Linked List,Find Occurrences,我的NB三明治类型方法有问题 它应该遍历双链接列表,并计算相同类型的sandwitch出现的次数,除了最后一个打印0的三明治之外,一切都很好,这是我不理解的,我的检查方法是说它不存在,但当我创建一个get last方法时,它确实存在。我的三明治方法缺少什么条件?我的while循环实际上没有到达最后一个节点吗 多谢各位 main class : Sandwich s1 = new Sandwich(1); Sandwich s1 = new Sandwich(1);

我的NB三明治类型方法有问题 它应该遍历双链接列表,并计算相同类型的sandwitch出现的次数,除了最后一个打印0的三明治之外,一切都很好,这是我不理解的,我的检查方法是说它不存在,但当我创建一个get last方法时,它确实存在。我的三明治方法缺少什么条件?我的while循环实际上没有到达最后一个节点吗

多谢各位

main class : 
    Sandwich s1 = new Sandwich(1);
        Sandwich s1 = new Sandwich(1);
        Sandwich s2 = new Sandwich(15);
        Sandwich s3 = new Sandwich(15);
        Sandwich s4 = new Sandwich(4);
        Sandwich s5 = new Sandwich(15);

        APreparer a1 = new APreparer();
        a1.addfirst(s1); 
        a1.addfirst(s2);
        a1.addfirst(s3);
        a1.addfirst(s4);
        a1.addfirst(s5);

        System.out.println(a1.nbSandwichs(15)); // PRINTS : 3 OK 
        System.out.println(a1.nbSandwichs(1)); // PRINTS : 0 NOT OK 


    public class Sandwich {

        private int type;

        public Sandwich(int type) {
            this.type = type;
            commandes[type]++;
        }

    public class APreparer {

        private UneCommande first;
        private UneCommande last;

        public void addfirst(Sandwich sandwich) {
            UneCommande nouvelle = new UneCommande(sandwich);
            if (first == null) {
                first = nouvelle;
                last = nouvelle;

            } else {
                first = first.addFirst(sandwich);
            }
        }

    int nbSandwichs(int type) {
        if (first == null) {
            return 0;
        } else {
            return first.nbSandwichs(type);
        }
    }

    }


    public class UneCommande {

        private Sandwich sandwich;
        private UneCommande next;
        private UneCommande previous;

        public UneCommande(Sandwich sandwich) {
            this.sandwich = sandwich;
        }

        public UneCommande addFirst(Sandwich sandwich) {
            UneCommande current = this;
            UneCommande newSand = new UneCommande(sandwich);
            newSand.next = current;
            this.previous = newSand;

            return newSand;
        }
int nbSandwichs(int type) {
        int counter = 0;
        UneCommande current = this;

        if (!(check(type))) {
            return 0;
        } else {
            while (current.next != null) {
                if (current.sandwich.getType() == type) {
                    counter++;
                }
                current = current.next;
            }
        }
        return counter;
    }

    boolean check(int type) {
        UneCommande current = this;
        while (current != null) {
            if (current.sandwich.getType() == type) {
                System.out.println("EXIST");
                return true;
            }
            current = current.next;
        }

        return false;
    }
}

循环计算节点的长度与当前相同。下一步!=无效的当current是列表中的最后一个节点时,current.next将为空,因此不计算在内。

谢谢!!我已经在while循环之外添加了if current.sandwich.getType==type{counter++;},现在可以工作了一个更好的解决方案是将测试更改为当前无效的