如何显示两个对象彼此相邻?

编程入门 行业动态 更新时间:2024-10-17 07:38:53
本文介绍了如何显示两个对象彼此相邻?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在一个文件中读到一个字母(City)与另一个字母相邻。它们由一个标签分隔。

I have to read in a file that shows one letter (City) is adjacent to the other. They are separated by a tab.

如何显示Q与X相邻,R与X(也)相邻,P与R相邻等......?

How do I show that Q is adjacent to X, R is adjacent to X (also), P is adjacent to R, etc etc...?

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

要在文件中读取的代码段:

Segment of code to read in the file:

private ArrayList<City> cityList; private ArrayList<City> cityFromList; private ArrayList<City> cityToList; Scanner theFlightFile = null; try { theFlightFile = new Scanner (new File("flightFile.txt")); } catch (Exception FileNotFoundException) { System.out.println(FileNotFoundException.getMessage()); } 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); }

方法显示与'aCity相邻的所有城市的名称。 @param aCity需要邻接列表的城市。

Method to that displays the names of all the cities which are adjacent to 'aCity'. @param aCity The city for which the adjacency list is desired.

//this is completely wrong, I know... public void displayAdjacentCities(City aCity) { //for (aCity : cityFromList) { // for (City cityB : cityToList) { // System.out.println(cityA + " is adjacent to " + cityB); // } //} }

推荐答案

您可以使用 HashMap< City,HashSet< City>> 结构,其中键是城市对象,它指向到一组相邻的城市。

You can use a HashMap<City, HashSet<City>> structure where the key is a city object and it points to a set of its adjacent cities.

更多推荐

如何显示两个对象彼此相邻?

本文发布于:2023-11-29 21:28:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!