这是我的代码:

public class FlightMap implements FlightMapInterface {

LinkedList<City> cityList = new LinkedList<City>();
LinkedList<City> nextCity = new LinkedList<City>();

/**
 * Creates an empty FlightMap
 */
public FlightMap() {
}


public void loadFlightMap(String cityFileName, String flightFileName)
        throws FileNotFoundException {

    File cityList = new File(cityFileName);
    File flightsTo = new File (flightFileName);

    Scanner theCityFile = null;
    Scanner theFlightFile = null;
    Scanner theRequestFile = null;

    ArrayList<String> cityStringList = new ArrayList<String>();
    int counter = 0;

    try {
        theCityFile = new Scanner (cityList);
        theFlightFile = new Scanner (flightsTo);
    }
    catch (FileNotFoundException e) {
        System.out.println("No such file exists.");
    }

    while (theFlightFile.hasNextLine()) {
        cityStringList.add((theFlightFile.nextLine()).replaceAll("\t", ""));
    }

    while (theCityFile.hasNextLine()) {
        LinkedList<City> tempList = new LinkedList<City>();
        String tempCity = theCityFile.nextLine();
        nextCity.add(tempList); // ERROR
        nextCity.get(counter).add(new City(tempCity)); // ERROR

        for (int x = 0; x < cityStringList.size(); x++) {
            if (cityStringList.get(x).charAt(0) == tempCity.charAt(0)) {
                insertAdjacent(nextCity.get(counter).getFirst(), // ERROR
                        new City(cityStringList.get(x)).charAt(1) + "");  // ERROR
            }
        }
        cityList.add(new City(tempCity)); // ERROR
        counter++;
    }
}


对于我的错误:


线程“主”中的异常java.lang.Error:未解决的编译
问题:
LinkedList类型的方法add(City)不适用于参数(LinkedList)
未为城市类型定义方法add(City)
对于城市类型,未定义方法getFirst()
对于类型City,未定义方法charAt(int)

最佳答案

错误确切地说。

LinkedList<City> tempList = new LinkedList<City>();
String tempCity = theCityFile.nextLine();
nextCity.add(tempList);


应该是

String tempCity = theCityFile.nextLine();
nextCity.add(tempCity);


您试图将LinkedList添加到另一个LinkedList。您只能将类型为City的“节点”添加到声明的LinkedList中。

09-30 17:28
查看更多