首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的Linkedlist的第一层不打印?

为什么我的Linkedlist的第一层不打印?
EN

Stack Overflow用户
提问于 2013-04-23 14:44:54
回答 1查看 177关注 0票数 0

我试图通过链表来显示两个对象之间的邻接关系。当我试图打印出邻接关系时,它跳过了第一行。下面是打印出来的内容:

代码语言:javascript
复制
X



X

R   >>>   W

S   >>>   Y

T

W

R   >>>   Z

它应该是:

代码语言:javascript
复制
Q   >>>   X
R   >>>   x
P   >>>   R   >>>   W
W   >>>   S   >>>   Y
S   >>>   T
T   >>>   W
Y   >>>   R   >>>   Z

如您所见,只有第二层和第三层被打印出来,而不是全部打印出来。

相关代码:

代码语言:javascript
复制
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类

代码语言:javascript
复制
//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();
}

显示邻接方法:

代码语言:javascript
复制
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中:(每个对象都在单独的一行上)

代码语言:javascript
复制
Q
X
R
P
W
S
T
Y
Z

邻接关系位于flightFile中(由两个字母之间的制表符和单独行上的each对分隔:

代码语言:javascript
复制
Q   X
R   X
P   R
P   W
W   S
S   T
T   W
W   Y
Y   R
Y   Z

所需路径在requestFile中:

代码语言:javascript
复制
Q   X
P   X
P   T
P   Z
P   X
P   Q
T   P
A   S
R   X
T   X
Q   X

我不确定我问的问题是否正确。

EN

Stack Overflow用户

回答已采纳

发布于 2013-04-23 15:06:53

样本答案看起来好像一个城市应该被认为是与它自己相邻的?试试这个:

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16162686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档