如何显示两个对象相邻?

时间:2013-04-21 06:14:15

标签: java

我今天早些时候问过这个问题,但答案对我的问题没有帮助。以为我可以再次询问更新,希望更接近我的问题。

如何显示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'相邻

2 个答案:

答案 0 :(得分:1)

@trama
如果您有兴趣,这是使用HashMap的实现。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestAdj {

    HashMap<String, ArrayList<String>> map;


    public TestAdj() {
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader("input.txt"));
            map = new HashMap<String, ArrayList<String>>();
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] set = line.split("\t");
                if (map.containsKey(set[0])) {
                    map.get(set[0]).add(set[1]);
                } else {
                    ArrayList lst = new ArrayList<String>();
                    lst.add(set[1]);
                    map.put(set[0], lst);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(TestAdj.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(TestAdj.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void displayAdj() {
        Object[] sources=map.keySet().toArray();

        for (int i = 0; i < sources.length; i++) {
            System.out.print(sources[i]+" -->");
            System.out.println(map.get(sources[i]));
        }
    }

    public static void main(String[] args) {
        new TestAdj().displayAdj();
    }
}

答案 1 :(得分:-1)

你尝试使用某种地图存储你的清单怎么样?

// Storing the cities:
HashMap<City, LinkedList<City>> cityList = new HashMap<City, LinkedList<City>>();
String cityFrom = theFlightFile.next();
String cityTo = theFlightFile.next();
City cityA = new City(cityFrom);
City cityB = new City(cityTo);

LinkedList<City> currentFollowers;
if (!cityList.containsKey(cityA)) {
     currentFollowers = new LinkedList<City>();
     cityList.put(cityA, currentFollowers);
} else {
     currentFollowers = cityList.get(cityA);
}

currentFollowers.add(cityB);
cityList.put(cityA, currentFollowers);

// For the output you could still use a String:
public void displayAdjacentCities(City aCity) {
     String output = aCity + " is adjacent to";
     for(City cityTo : cityList.get(aCity)) {
          output += " " + cityTo;
     }
     System.out.println(output);
}

// and your displayFlightMap-Method could look like:    
for(City from : cityList.keySet()) {
    displayAdjacentCities(from);
}