Java 如何比较通用LinkedList中的对象

Java 如何比较通用LinkedList中的对象,java,generics,singly-linked-list,Java,Generics,Singly Linked List,如何比较通用链表中的对象?我从头开始写我的链表。我有一个名为Country的类,它从csv文件中读取并存储国家名称及其特定年份的cellular stat数据。上面写的是1960年至2012年的数据。我的程序运行良好,因为我能够打印出列表。但当我搜索列表以匹配控制台中打印的国家名称时。上面说没找到。我是泛型新手,当我试图重写类国家中的equals方法来比较名称时,我感到有些不对劲。但是我在班级国家里的equals方法没有被调用。我试着调试equals方法,看看是否通过放置print-out语句调

如何比较通用链表中的对象?我从头开始写我的链表。我有一个名为Country的类,它从csv文件中读取并存储国家名称及其特定年份的cellular stat数据。上面写的是1960年至2012年的数据。我的程序运行良好,因为我能够打印出列表。但当我搜索列表以匹配控制台中打印的国家名称时。上面说没找到。我是泛型新手,当我试图重写类国家中的equals方法来比较名称时,我感到有些不对劲。但是我在班级国家里的equals方法没有被调用。我试着调试equals方法,看看是否通过放置print-out语句调用过它,但从未调用过。我猜是因为使用了泛型。请帮我解决这个问题。这是我在应用程序中遇到的一个问题,因为我的contains方法根本不比较国家名称

类链表

public class LinkedList<T> {

private Node<T> first;

public LinkedList()
{
    this.first = null;
}

//returns true if list is empty
public boolean isEmpty()
{
    return (first==null);
}
//adds the node to the end of the list
public void add(T obj)
{
    Node<T> newNode = new Node<T>(obj);
    if(this.isEmpty())
    {
        newNode.setNode(first);
        first = newNode;
    }
    else
    {
        Node<T> current = first;
        while(current.getNext()!=null)
        {
            current = current.getNext();
        }
        current.setNode(newNode);
    }
}

public String toString()
{
    String result ="";
    if(this.isEmpty())
    {
        result += "Cannot print an empty list";
        return result;
    }
    else
    {
        Node<T> current = first;
        while(current!=null)
        {
            result +=current.getObj();
            current = current.getNext();
        }
        return result;
    }
}
//searches for the countryName and returns the country if found
public T contains(T obj)
{
    if(this.isEmpty())
    {
        return null;
    }
    else
    {
        Node<T> current = first;
        while(current!=null)
        {
            if(current.getObj().equals(obj))
            {
                return current.getObj();
            }
            current= current.getNext();
        }
        return null;
    }
}
}
}

方法,该方法调用函数以比较节点中的国家名称

private void testRandomListOfCountries(Country [] allCountries)
{   
    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many countries do you want to add to the list?");
    int requestedSize = Integer.parseInt(keyboard.nextLine());


    // Build the list out of a random selection of countries.
    Random random = new Random();
    LinkedList<Country> selectedCountries = new LinkedList<Country>();
    for (int i = 0; i < requestedSize; i++)
    {
        int selectedIndex = random.nextInt(allCountries.length);
        selectedCountries.add(allCountries[selectedIndex]);
    }


    // Note: To debug your list, comment this line in
    System.out.println("List of countries: " + selectedCountries);


    // Check if the name of a country is in the list.
    // If the country is found, print the details.
    // Otherwise output not found.
    System.out.println("\nWhat country do you want to search for?");
    String countryToFind = keyboard.nextLine();
    //Country obj = new Country(countryToFind);
    Country foundCountry = selectedCountries.contains(new Country(countryToFind));
    if (foundCountry != null)
    {
        System.out.println("Country " + countryToFind + " found with details:\n" + foundCountry);
    }
    else
        System.out.println("Country " + countryToFind + " not found.");

}

当您重写
equals
时,它必须是
equals(Object)
,而不是
equals(YourClassName)
。您实际上还没有重写该方法;您使用相同的名称创建了一个单独的方法,
LinkedList
不会调用该方法。

以新的响应进行应答,以便我可以引用代码

回来,拉拉。这个特殊的问题不是泛型,而是如何重写java.lang.Object中定义的键方法
equals(Object)
。要重写基类中的方法,必须获得签名点。很容易搞糟,java在某处(Java1.4??)添加了方法注释
@Override
。这会告诉编译器(以及所有阅读代码的人…包括明天的您)“此方法旨在重写基类(或接口)中的方法;如果不是,请抛出一个编译时错误,因为我有f-ed

按照此指导原则,您应该键入:

//overrides the equals method and returns country name if found
@Overrides
public boolean equals(Country obj)
...
编译器将跳入并将您保存在此处,注意它不会覆盖任何内容。正确的做法是:

//overrides the equals method and returns country name if found
@Overrides
public boolean equals(Object obj)
...
我想补充一点……您已经提交了一个主要java开发人员sin。您已经[尝试]编写了
equals()
,但您还没有覆盖
hashCode()
(再次指定
@Override
在这里更为重要(我个人经常忘记大写“C”)说真的,千万不要写这两种方法中的一种而不写另一种

作为此答案的作业,请仔细阅读以下内容:


是的。我用来避免这个错误的一种做法是,当我打算重写基类方法时,我使用:
@Overrides
来记录意图,并且当我犯这样的错误时,编译器会打我的头。@user2357112非常感谢。为什么会出现这个错误?如果你能解释一下,仍然有点困惑le bit。对java来说还是新的generics@BrianT.为什么会出现这样的错误?还是有点困惑,而且我对泛型还不熟悉。还有,您如何使用“@overrides”作为一种帮助reminder@LALA...response比“评论”更完整的回答。
How many countries do you want to add to the list?
4
 European Union 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.010455902 0.014641232 0.022017047 0.033736759 0.059970029 0.099394983 0.172494402 0.290883796 0.468609861 0.647271896 0.88803523  1.180245548 1.773778201 2.896911981 4.555055819 7.189007007 11.51481484 19.70441779 33.91179565 53.57287113 64.80565912 70.93170081 77.89306636 85.94063784 95.79155903 105.012288  115.1562612 120.8409684 121.1416294 119.2250553 121.9304985 123.7159497 
 Sint Maarten (Dutch part)  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
 Belize 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.201592581 0.411172831 0.747480214 1.028369629 1.164387324 1.570531759 2.842663676 7.046515722 15.96872731 20.54645981 23.38066005 28.29761545 35.30450132 42.2961808  41.34020042 54.51721037 53.74564807 62.93070205 70.31965347 53.20712214 
 Uganda 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.008423042 0.018684872 0.022640286 0.131691862 0.239741703 0.522799789 1.130100554 1.516028656 2.892006194 4.195756068 4.578959089 6.761102482 13.65261655 26.92003559 28.55294653 37.74438345 47.50472743 45.00206351 
 List of countries: 
 What country do you want to search for?
 Belize
 Country Belize not found.
//overrides the equals method and returns country name if found
@Overrides
public boolean equals(Country obj)
...
//overrides the equals method and returns country name if found
@Overrides
public boolean equals(Object obj)
...