我必须在一个文件中读到一个字母(City)与另一个字母相邻。它们由一个标签分隔。
I have to read in a file that shows one letter (City) is adjacent to the other. They are separated by a tab.
如何显示Q与X相邻,R与X(也)相邻,P与R相邻等......?
How do I show that Q is adjacent to X, R is adjacent to X (also), P is adjacent to R, etc etc...?
Q X R X P R P W W S S T T W W Y Y R Y Z要在文件中读取的代码段:
Segment of code to read in the file:
private ArrayList<City> cityList; private ArrayList<City> cityFromList; private ArrayList<City> cityToList; Scanner theFlightFile = null; try { theFlightFile = new Scanner (new File("flightFile.txt")); } catch (Exception FileNotFoundException) { System.out.println(FileNotFoundException.getMessage()); } while (theFlightFile.hasNext()) { String cityFrom = theFlightFile.next(); String cityTo = theFlightFile.next(); City cityA = new City(cityFrom); City cityB = new City(cityTo); cityToList.add(cityA); cityFromList.add(cityB); //testing input reading... System.out.println(cityFrom + " -----> " + cityTo); }方法显示与'aCity相邻的所有城市的名称。 @param aCity需要邻接列表的城市。
Method to that displays the names of all the cities which are adjacent to 'aCity'. @param aCity The city for which the adjacency list is desired.
//this is completely wrong, I know... public void displayAdjacentCities(City aCity) { //for (aCity : cityFromList) { // for (City cityB : cityToList) { // System.out.println(cityA + " is adjacent to " + cityB); // } //} }推荐答案
您可以使用 HashMap< City,HashSet< City>> 结构,其中键是城市对象,它指向到一组相邻的城市。
You can use a HashMap<City, HashSet<City>> structure where the key is a city object and it points to a set of its adjacent cities.
更多推荐
如何显示两个对象彼此相邻?
发布评论