0

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

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);
    }

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);
    //  }
    //}

}
trama
  • 1,271
  • 3
  • 14
  • 24
  • 1
    Looks like you will need an [adjacency matrix](http://en.wikipedia.org/wiki/Adjacency_matrix) – Luiggi Mendoza Apr 20 '13 at 21:36
  • @LuiggiMendoza I havent learned anything about that yet. Don't know if I am allowed to use it. – trama Apr 20 '13 at 21:37
  • are you learning about programming? Do you know about `List`, `Set` and `Map` interface? Do you know about arrays and array of arrays (commonly known as matrix)? Then you know the basics to create an adjacency matrix. – Luiggi Mendoza Apr 20 '13 at 21:38
  • @LuiggiMendoza I know those interfaces and matrix. I mean I'll definitely look into, but I dont know I am allowed to use something that we haven't learned yet. My teacher is anal like that. – trama Apr 20 '13 at 21:41
  • Well, you just read the concept and learn to implement one. Will your teacher be mad about you learning something by yourself? – Luiggi Mendoza Apr 20 '13 at 21:41
  • @LuiggiMendoza she better not be. – trama Apr 20 '13 at 21:42

2 Answers2

1

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.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • You mean `Map>` backed by `LinkedHashMap>`. Also, it will be worth to mention that `City` class must override `equals` and `hashcode` methods in order to make this approach to work. – Luiggi Mendoza Apr 20 '13 at 21:40
  • Better `HashMap>` ... It would be faster to test. – Valeri Atamaniouk Apr 20 '13 at 21:42
  • @LuiggiMendoza you mention trivial things to use that, because I mention a map implementation and the OP is supposed to know that already. – Juvanis Apr 20 '13 at 21:42
  • @ValeriAtamaniouk a better option would even be `Set` where `City` contains a `Set adjacentCities`. – Luiggi Mendoza Apr 20 '13 at 21:42
  • @Juvanis maybe not, we don't know for sure if OP knows about `Set` (at least not before the comment on question). – Luiggi Mendoza Apr 20 '13 at 21:43
  • One more thing, it would be better for you to check this link: [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197) – Luiggi Mendoza Apr 20 '13 at 21:46
0

The easiest way would be:

for (i = 0; i < cityFromList.length(); i++) {
  if (cityFromList.get(i).equals(aCity)) {
    System.out.println(aCity + " is adjacent to " + cityToList.get(i) + ".");
  }
}

This of course requires that the "City" class would need to implement the equals method and hence the hashcode method as well.

Arjun Rao
  • 677
  • 4
  • 15
  • I have a City class, but didnt include it. I didn't think it would be relevant to what I have – trama Apr 20 '13 at 21:44