-4

I've asked this question earlier today, but the answers didn't help me with my problem. Thought I could ask again with updated and hopefully closer to do my problem.

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

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

So it'd print to the screen:

Q is adjacent to X
R is adjacent to X
P is adjacent to R (tab or spaces) W
etc etc etc

Segment of code to read in the file and store them into two different ListArray's

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

What prints is what looks like cityToList printing 10 times and it all saying its adjacent to 'Z'

trama
  • 1,271
  • 3
  • 14
  • 24
  • 4
    No no no, if the answers given aren't good, just say so and wait for new answers. You can't ask the question again. – Mr Lister Apr 21 '13 at 06:22
  • This is costly when you have a huge set of inputs. Use a `HashMap` with `ArrayList` as values which contains the adjacent values to the key of the `HashMap`. Ex: `HashMap >` – Praneeth Peiris Apr 21 '13 at 06:22
  • @MrLister I didn't know if anybody else was going to answer... Trust me, I debated posting this for a solid 30 minutes... – trama Apr 21 '13 at 06:25
  • @GnomezGrave I think I am going to try HashMap again. I didn't seem to work the first time I did it. – trama Apr 21 '13 at 06:26
  • @trama Its not good to have dupplicate posts. Delete other posts, at least. – Ezhil V Apr 21 '13 at 06:35
  • @javapirate Am I allowed to just delete posts? I thought you account gets locked for that. – trama Apr 21 '13 at 06:36
  • 1
    *"Trust me, I debated posting this for a solid 30 minutes..."* ... and the wrong side won the debate!! Don't repost Questions. If you feel your Question is not receiving enough attention: 1) Update it to improve it and make it easier for people to understand, and/or note why the existing Answers don't help. 2) Post a Bonus. This requires reputation points ... so you may need to *earn some* by answering Questions etc. – Stephen C Apr 21 '13 at 06:40
  • Deleting your old Question is bad form too ... unless it was so bad that it was misleading and unanswerable. Why? Because the Answers to deleted Questions ... that other people spent time writing ... also get deleted. Even if they don't help you, they might help someone else. – Stephen C Apr 21 '13 at 06:41
  • @StephenC I understand. I didnt know if I updated that others will still look at it. – trama Apr 21 '13 at 06:45
  • Stephen is right; if you edit your question, it will return to the top of the newest questions list, so it will get new eyeballs. Putting up a bonus is not an option now, but you can do that in the future. – Mr Lister Apr 21 '13 at 07:20
  • @MrLister I didn't know it gets moved back to the top. Thank you for the heads up though – trama Apr 21 '13 at 07:22

2 Answers2

1

@trama
If you are interested, here is the implementation using 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();
    }
}
Praneeth Peiris
  • 2,008
  • 20
  • 40
-1

How about you try to use some kind of map to store your list?

// 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);
}
dicheridoo
  • 89
  • 3