2013-04-23 130 views
0

我想通過鏈表顯示兩個對象之間的鄰接關係。當我試圖打印出鄰接時,它跳過第一行。這是打印出:爲什麼我的「鏈接列表」的第一層不打印?

X 



X 

R >>> W 

S >>> Y 

T 

W 

R >>> Z 

而且它應該是:

Q >>> X 
R >>> x 
P >>> R >>> W 
W >>> S >>> Y 
S >>> T 
T >>> W 
Y >>> R >>> Z 

正如你所看到的,只有第二級和第三級打印出來,而不是所有的人。

相關的代碼:

public static void main(String[] args) throws FileNotFoundException { 

    FlightMap flights = new FlightMap(); 
    flights.loadFlightMap("cityFile.txt", "flightFile.txt"); 
    Scanner requestInStream = null; 

    requestInStream = new Scanner(new File("requestFile.txt")); 

    //DEBUGGING 
    String[] str = new String[] {"Q", "X", "R", "P", "W", "S", "T", "Y", "Z"}; 
    String temp = null; 
    for (int x = 0; x < str.length; x++) { 
     temp = str[x]; 
     City city = new City(temp); 
     flights.displayAdjacentCities(city); 
    } 

FlightMap類

//Read in the files from the main... 
private int size = 0; 
private City[] allCities; 
private LinkedList<City>[] adjacents; 

public FlightMap() { 
} 

@SuppressWarnings("unchecked") 
public void loadFlightMap(String cityFileName, String flightFileName) 
     throws FileNotFoundException { 


    Scanner inStream = null; 
    inStream = new Scanner(new File(cityFileName)); 
    while(inStream.hasNextLine()) { 
     size++; 
     inStream.nextLine(); 
    } 
    inStream.close(); 
    allCities = new City[size]; 
    adjacents = (LinkedList<City>[]) new LinkedList[size]; 

    for(int x = 0; x < size; x++) { 
     adjacents[x] = new LinkedList<City>(); 
    } 

    Scanner inStream1 = null; 
    inStream1 = new Scanner (new File(cityFileName)); 
    for(int x = 0; x <size; x++) { 
     String input = inStream1.nextLine(); 
     allCities[x] = new City (input); 
    } 

    inStream1 = new Scanner (new File(flightFileName)); 
    String data; 
    while (inStream1.hasNext()) { 
     int flag = 0; 
     data = inStream1.next(); 
     for (int i = 0; i < size && flag !=1; i++) { 
      if(allCities[i].getName().equalsIgnoreCase(data)) { 
       data = inStream1.next(); 
       for (int j = 0; j < size; j++) { 
        if (allCities[j].getName().equalsIgnoreCase(data)) { 
         adjacents[i].add(allCities[j]); 
         flag = 1; 
        } 
       } 
      } 
     } 
    } 
    inStream1.close(); 
} 

顯示鄰接方法:

public void displayAdjacentCities(City aCity) { 
    for(int x = 0; x < size; x++) { 
     if (aCity.getName().equalsIgnoreCase(allCities[x].getName())) { 
      Iterator<City> iter = adjacents[x].iterator(); 
      while(iter.hasNext()) { 
       System.out.print(iter.next().getName()); 
       if(iter.hasNext()) { 
        System.out.print(" >>> "); 
       } 
      } 
      System.out.println("\n"); 
     } 
    } 

所有對象都在cityFile: (每個在單獨的行)

Q 
X 
R 
P 
W 
S 
T 
Y 
Z 

鄰接處於flightFile(由和各對單獨的行上兩個字母之間的製表符分隔:

Q X 
R X 
P R 
P W 
W S 
S T 
T W 
W Y 
Y R 
Y Z 

和期望的路徑是在requestFile:

Q X 
P X 
P T 
P Z 
P X 
P Q 
T P 
A S 
R X 
T X 
Q X 

我我不確定我是否問正確的問題。

+0

請突出顯示您遇到問題的區域。 – 2013-04-23 06:47:27

+0

@Nikhil我認爲我正在閱讀文件錯誤...或者我的鄰接方法不正確。我不知道如何實際測試其中之一。城市排列,讀取正常,但我不確定flightFile。 – user1864651 2013-04-23 06:51:36

+0

你嘗試過調試嗎?這是一個非常有用和必須學習的工具。 – BobTheBuilder 2013-04-23 06:53:40

回答

0

樣本答案看起來好像一個城市應該被認爲與自己相鄰? 試試這個:

public void displayAdjacentCities(City aCity) { 
    for(int x = 0; x < size; x++) { 
    if (aCity.getName().equalsIgnoreCase(allCities[x].getName())) { 
     if (adjacents[x].size() == 0) continue; 
     System.out.print(aCity.getName()); 
     for (City other : adjacents[x]) { 
       System.out.print(" >>> " + other.getName()); 
      } 
      System.out.println(); 
     } 
    } 
}