Java 需要帮助搜索链接列表吗

Java 需要帮助搜索链接列表吗,java,linked-list,Java,Linked List,大家好,我在搜索链接列表时遇到问题。基本上,我是从csv文件中读取数据并将其存储在链表中。我能够在最后添加列表。但当我搜索列表时,它总是说没有找到。方法函数被称为contains。一个方法“contains”,它将Country对象作为参数,并检查是否可以在列表中找到国家的名称。要检查Country类型的object foo是否等于Country类型的object bar,必须重写类Country中的“equals方法”。当我运行代码时,它返回not found,我发现该方法包含的类countr

大家好,我在搜索链接列表时遇到问题。基本上,我是从csv文件中读取数据并将其存储在链表中。我能够在最后添加列表。但当我搜索列表时,它总是说没有找到。方法函数被称为contains。一个方法“contains”,它将Country对象作为参数,并检查是否可以在列表中找到国家的名称。要检查Country类型的object foo是否等于Country类型的object bar,必须重写类Country中的“equals方法”。当我运行代码时,它返回not found,我发现该方法包含的类countryNode返回null,这就是它返回not found的原因。谢谢你的帮助。除了contains方法之外,其他方法都可以工作。下面是我的代码:

public Country contains(Country obj)
{

   if(this.isEmpty())
   {
       System.out.println("Sorry this is an Empty list");
       return null;
   }
   else{
       CountryNode current = first;
        while(current!=null)
       {
           if(current.getCountry().equals(obj))
           {
                return current.getCountry();
               // break;
           }
           current = current.getNext();
       }
      return null;

   }     
}
类国家和覆盖方法等于:

public class Country {

private String countryNames;
private SubscriptionYear[] subscriptions;
private int size;
private int location;

public Country(String country)
{
    this.countryNames = country;
}
public Country(String country, int arraylength)
{
    this.countryNames = country;
    this.size = arraylength;
    subscriptions = new SubscriptionYear[size];
    location = 0;
}

public void addSubscriptionYear(int year, double subscription)
{
        subscriptions[location]= new SubscriptionYear(year, subscription);
        ++location;
}


public String toString()
{
    System.out.print(countryNames+"\t");
    for(SubscriptionYear s: subscriptions)
    {
        //System.out.print(countryNames+"\t");
        System.out.print(s.getSubscription()+"\t");
    }
    System.out.println();
    return  "";
}
 public String getName()
 {
    return this.countryNames;
 }

public boolean equals(Country obj)
{
    return (this.countryNames==obj.countryNames);
}
}
这是我的测试主文件:

import java.util.Random;
import java.util.Scanner;

 public class TestCountryList 
 {
  /**
  * Builds a list of countries to debug.
  */
  private void debugListOfCountries(Country [] allCountries)
  {
    // TO COMPLETE
  }

  /**
  * Builds a random list of countries.
  */
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 = keyboard.nextInt();

    // Build the list out of a random selection of countries.
    Random random = new Random();
    CountryList selectedCountries = new CountryList();
    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.next();
    Country obj = new Country(countryToFind);
    Country foundCountry = selectedCountries.contains(obj);
    if (foundCountry != null)
    {
    System.out.println("Country " + countryToFind + " found with details:" + foundCountry);
    }
    else
        System.out.println("Country " + countryToFind + " not found.");

}

/**
 * Includes test examples for class GraphView.
 */
public static void main(String[] args) 
{
    // Create and set objects of type Country 
    //
    final String FILENAME = "data/cellular.csv";    // Directory path for Mac OS X
    //final String FILENAME = "data\cellular.csv";  // Directory path for Windows OS (i.e. Operating System)
    final int NUM_COUNTRIES_TO_TEST = 3;            // Note: Include test cases in addition to 3


    // Parse the CSV data file
    //
    CSVReader parser = new CSVReader(FILENAME);

    String [] countryNames = parser.getCountryNames();
    int [] yearLabels = parser.getYearLabels();
    double [][] parsedTable = parser.getParsedTable();      


    // Create and set objects of type Country 
    //
    Country [] countries;
    countries = new Country[NUM_COUNTRIES_TO_TEST];

    Country current;
    countries = new Country[countryNames.length];

    for (int countryIndex = 0; countryIndex < countries.length; countryIndex++)
    {
        int numberOfYears = yearLabels.length;   // OR numberOfYears = dataTable[countryIndex].length;

        current = new Country(countryNames[countryIndex], numberOfYears);

        for (int yearIndex = 0; yearIndex < numberOfYears; yearIndex++)
        {
            double [] allSubscriptions = parsedTable[countryIndex];
            double countryData = allSubscriptions[yearIndex];
            current.addSubscriptionYear(yearLabels[yearIndex], countryData);
        }
        countries[countryIndex] = current;
    }

    TestCountryList application = new TestCountryList();

    // Note: Initially, to test your output you may hard code the number of 
    //       countries added, and the array positions selected.
    //       However, make sure to comment this out before submitting your work.
    //application.debugListOfCountries(countries);

    application.testRandomListOfCountries(countries);
}
}
import java.util.Random;
导入java.util.Scanner;
公共类TestCountryList
{
/**
*生成要调试的国家/地区列表。
*/
国家/地区(国家[]所有国家/地区)
{
//完成
}
/**
*建立一个随机的国家列表。
*/
国家/地区(国家[]所有国家/地区)
{   
扫描仪键盘=新扫描仪(System.in);
System.out.println(“您希望将多少国家/地区添加到列表中?”);
int requestedSize=keyboard.nextInt();
//从随机选择的国家中建立列表。
随机=新随机();
CountryList selectedCountries=新建CountryList();
对于(int i=0;i
您需要使用
equals
equalsIgnoreCase
来比较字符串

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

尝试覆盖对象的equals方法,如下所示:

public boolean equals(Object obj)
{
   if (this == obj) {
       return true;
   }
   if (obj == null) {
     return false;
   }
   if (getClass() != obj.getClass()) {
      return false;
   }
   return (this.countryNames.equals(((Country)obj).countryNames));
}
内部包含call countryList.equals方法,equals方法的签名为

 public boolean equals(Object obj) {}
相对于

 public boolean equals(Country obj) {}
另外,您只是比较两个字符串引用,而需要比较字符串的内容。所以不是

 this.countryNames==obj.countryNames
你应该说:

 this.countryNames.equals(obj.countryNames);

请阅读您在这里发布的长代码。为了你自己的利益,试着缩短它。你并不是凌驾于对象之上。非常感谢你的澄清。我忘记了地址的比较,而不是内容的比较。