简体   繁体   English

这个contains(T entry)方法有什么问题? Java。 链表

[英]What is wrong with this contains(T entry) method? Java. Linked List

I'm making a linked list class and am trying to implement this contains() method. 我正在制作一个链表类,并试图实现这个contains()方法。

I have head and tail sentinel nodes, so I have the loop start at head.next . 我有headtail的前哨淋巴结,所以我必须在循环开始head.next length is the size of the list. length是列表的大小。 Bout all I can give you guys :O 尽我所能给你们:O

public boolean contains(T entry) {

    boolean found = false;
    Node current = head.next;

    for (int i = 0; i < length; i++) {

        if (current.equals(entry)) {

            found = true;
        }
        current = current.next;
    }
    return found;
}

The problem is you're comparing a Node to a T which if your equals method correctly fufills the correct contract will always return false being different classes. 问题是您正在将NodeT进行比较,如果您的equals方法正确实现了正确的约定,则该T将始终返回false,因为它们是不同的类。

ie re-examine this line: 即重新检查这一行:

if (current.equals(entry)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM