我今天早些时候问过这个问题,但答案对我的问题没有帮助。以为我可以再次询问更新并希望更接近解决我的问题。
我如何证明 Q 与 X 相邻,R 与 X 相邻(也),P 与 R 相邻,等等……?文本文件
Q X
R X
P R
P W
W S
S T
T W
W Y
Y R
Y Z
所以它会打印到屏幕上:
Q is adjacent to X
R is adjacent to X
P is adjacent to R (tab or spaces) W
etc etc etc
读取文件并将它们存储到两个不同的 ListArray 中的代码段
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);
}
/**
* Displays to the screen, a list of all cities served by the airline
* along with the names of cities to which each is adjacent.
*/
public void displayFlightMap() {
int i = 0;
while (!cityStack.isEmpty() && topCity.equals(destinationCity)) {
displayAdjacentCities(cityFromList.get(i));
i++;
}
}
/**
* Displays to the screen, the names of all cities which are are adjacent
* to aCity; aCity is assumed to be a valid city served by the airline.
* @param aCity The city for which the adjacency list is desired.
*/
public void displayAdjacentCities(City aCity) {
String str = "";
for (City cityA : cityToList) {
for (City cityB : cityFromList) {
if (cityA != cityB) {
str = cityA + " is adjacent to " + cityB;
}
}
System.out.println(str);
}
}
打印的内容看起来像 cityToList 打印 10 次,并且都说它与“Z”相邻