我今天早些时候问过这个问题,但答案并没有帮助我解决我的问题。以为我可以再次提出更新,希望更接近我的问题。如何显示两个物体相邻?
如何显示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'
不,不,如果给出的答案不好,就直接说出来,等待新答案。你不能再问这个问题。 – 2013-04-21 06:22:21
当您拥有一大组输入时,这是昂贵的。使用带有'ArrayList'的'HashMap'作为包含'HashMap'键的相邻值的值。 例如:'HashMap>' –
2013-04-21 06:22:36
@MrLister我不知道其他人是否会回答...相信我,我辩论发布这个坚实的30分钟... – trama 2013-04-21 06:25:34