简体   繁体   English

带有链接列表的Java泛型问题

[英]Trouble with java generics with a link list

Hello guys i'm trying to search for a particular country in my list that i created. 大家好,我正尝试在我创建的列表中搜索特定的国家/地区。 First of all i was successfully able to create the list and able to print out to the console. 首先,我能够成功创建列表并能够打印到控制台。 but when i search for a country that was printed out on a console i tried entering one of the countries displayed on the screen to verify it and it displayed it wasn't found even when the displayed country is on the screen. 但是,当我搜索在控制台上打印的国家/地区时,我尝试输入屏幕上显示的国家/地区中的一个以进行验证,即使显示的国家/地区在屏幕上也找不到。 Please this is my first programming experience with generics in java> i have tried debugging it but to no avail. 请这是我第一次使用Java中的泛型进行编程,这是我的第一次编程经验,但我尝试对其进行调试,但无济于事。 Please could some one help. 请帮忙一下。 Node class holds a generic parameter and class LinkedList creates the list for node objects. Node类拥有一个通用参数,而LinkedList类则为节点对象创建列表。 Node class holds generic parameter that takes class country. 节点类拥有带类国家的通用参数。 The overridden method is written in class country that stores each country names. 覆盖的方法在存储每个国家/地区名称的国家/地区中编写。 I have put below the class LinkedList, class Node and the overridden method in class country as well as the search function in the driver class and the output received from the console. 我在类国家/地区下面放置了LinkedList类,Node类和重写的方法,以及驱动程序类中的搜索功能和从控制台接收的输出。

 public class LinkedList<T>{

private Node<T> first;

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

 public T contains(T obj)
{
    if(this.isEmpty())
    {
        System.out.println("Sorry list is empty");
        return null;
    }
    else
    {
        Node<T> current = first;
        while(current != null)
        {
            if(current.getT().equals(obj))
            {
                return current.getT();
            }
            current = current.getNext();
        }
        return null;
    }
}

Node class that has generic detail: 具有通用详细信息的节点类:

public class Node<T> {

private T data;
private Node<T> next;

public Node(T data){
    this.data = data;
    this.next = null;
}
public Node(T objectT, Node<T> next)
{
    this.data = objectT;
    this.next = next;
}
public void setNode(Node<T> next)
{
    this.next = next;
}
public Node<T> getNext()
{
    return this.next;
}
 public T getT()
{
    return data;
}
}

overriden equals method in a different class. 在另一个类中的equals equals方法。

 public boolean equals(Country obj)
{
    return this.countryNames.equalsIgnoreCase(obj.getName());   
}

my search function in the main class. 我在主要课程中的搜索功能。

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.");

}

Output below: 输出如下:

How many countries do you want to add to the list?
 4
 Turkmenistan   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.057656946 0.068254835 0.089899216 0.166614128 0.17955684  0.177667308 0.197653332 1.06666644  2.211532447 4.516582511 7.85614367  23.08368224 42.83804536 63.41981696 68.75324576 76.41702547 
 South Asia 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 
 Burkina Faso   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.00506143  0.014092851 0.024890963 0.044637705 0.217480374 0.637748952 0.902809026 1.880815092 3.037679629 4.720290206 7.354840819 13.05253397 20.62907931 25.3304628  36.72938024 48.02719397 60.60765215 
 Latin America and the Caribbean    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 
 List of countries: 

What country do you want to search for?
Turkmenistan
Country Turkmenistan not found.

Java Generics remove type information during compilation, and add casts for runtime typing. Java泛型在编译期间删除类型信息,并添加类型转换以进行运行时键入。 What this means is that your overriden equals() method is actually never called because it will call Object.equals() instead. 这意味着您实际上从未调用过重写的equals()方法,因为它将改为调用Object.equals() You can verify this by adding a print statement to the method or a breakpoint and observe that it never executes. 您可以通过在方法或断点上添加一条打印语句来验证这一点,并观察其永远不会执行。

The way to solve this is with another class called a Comparable (or Comparator). 解决此问题的方法是使用另一个称为Comparable (或Comparator)的类。 https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Implement the Comparable interface on your Country class, and change your conditional in your LinkedList to use the compareTo() method. 在Country类上实现Comparable接口,并在LinkedList中更改条件以使用compareTo()方法。 Your generic type declaration will also need to be changed to public class LinkedList<T extends Comparable> . 您的通用类型声明也将需要更改为public class LinkedList<T extends Comparable>

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

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