2014-11-05 111 views
0

大家好我无法搜索链接列表。基本上我从csv文件读取并将其存储在链接列表中。我能够在最后添加列表。但是,当我搜索列表时,它一直说它没有找到。该方法的功能被称为包含。一个方法“contains”将Country对象作为参数,并检查是否可以在列表中找到国家的名称。要检查Country的对象foo是否等于Country的对象栏,则必须覆盖Country Country中的“equals方法”。当我运行的代码它没有找到返回,我发现这个方法包含类countryNode返回null这就是为什么它的返回没有找到。我会感谢帮助,谢谢。除了包含方法外,一切都有效。下面是我的代码:需要帮助搜索链接列表

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; 

    }  
} 

类国家和覆盖equals方法:

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); 
} 
} 
+1

请阅读(http://stackoverflow.com/questions/513832/how-do-i- [我如何在Java中比较字符串]比较字符串在Java) – reto 2014-11-05 08:32:39

+0

你在这里发布了一个长的代码。尝试缩短它为你自己的好处。 – 2014-11-05 08:32:51

回答

1

尝试压倒一切等于对象的方法如下:

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)); 
} 

内部含有呼叫countryList.equals方法和equals方法的签名是

public boolean equals(Object obj) {} 

与之相对

public boolean equals(Country obj) {} 

还有你只需比较字符串的两个引用,而您需要比较String的内容。因此,而不是

this.countryNames==obj.countryNames 

你应该说:

this.countryNames.equals(obj.countryNames); 
+0

非常感谢您的澄清。我忘了地址比较而不是内容。 – 2014-11-05 09:07:21

1
你需要使用 equals

equalsIgnoreCase比较字符串

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

您不重写Object的equals。 – SMA 2014-11-05 08:41:02