大家好,我正在尝试在我创建的列表中搜索特定国家/地区。首先,我成功地创建了列表并能够打印到控制台。但是当我搜索在控制台上打印出来的国家时,我尝试进入屏幕上显示的某个国家/地区进行验证并显示即使显示的国家/地区在屏幕上也未找到。请问这是我在java中使用泛型的第一次编程经验>我试过调试但无济于事。请帮助一些人。 Node类包含一个泛型参数,而LinkedList类创建节点对象的列表。 Node类包含占用类country的泛型参数。重写的方法写在存储每个国家/地区名称的类国家/地区。我在类Country中设置了类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;
}
}
具有通用详细信息的节点类:
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;
}
}
覆盖等于另一个类中的方法。
public boolean equals(Country obj)
{
return this.countryNames.equalsIgnoreCase(obj.getName());
}
我在主类中的搜索功能。
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.");
}
以下输出:
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.
答案 0 :(得分:0)
Java Generics在编译期间删除类型信息,并为运行时输入添加强制转换。这意味着您的覆盖equals()
方法实际上从未被调用过,因为它会调用Object.equals()
。您可以通过向方法或断点添加print语句来验证这一点,并观察它永远不会执行。
解决此问题的方法是使用另一个名为Comparable
(或比较器)的类。 https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
在Country类上实现Comparable接口,并在LinkedList中更改条件以使用compareTo()方法。您的通用类型声明也需要更改为public class LinkedList<T extends Comparable>
。