1
votes

I have been asked to make a method to print an ArrayList. How do I make a method in a different class and then pull data from that class to print it in the Main class?

Example:

Class with the data:

public class GraphData {
    public ArrayList<Node> createGraph() {
        LinkedHashMap<String,Node> nodes = new LinkedHashMap<>();
        nodes.put("bole", new Node("Böle bibliotek",           60.2008, 24.9359));
        nodes.put("vall", new Node("Vallgårds bibliotek",      60.1923, 24.9626));
        nodes.put("berg", new Node("Berghälls bibliotek",      60.1837, 24.9536));
        nodes.put("tolo", new Node("Tölö bibliotek",           60.1833, 24.9175));
        nodes.put("oodi", new Node("Centrumbiblioteket Ode",   60.174,  24.9382));
        nodes.put("rich", new Node("Richardsgatans bibliotek", 60.1663, 24.9468));
        nodes.put("bush", new Node("Busholmens bibliotek",     60.16,   24.9209));


        HashMap<String,String[]> neighbours = new HashMap<>();
        neighbours.put("bole", new String[]{"tolo", "berg"});
        neighbours.put("vall", new String[]{"berg"});
        neighbours.put("berg", new String[]{"bole", "vall", "tolo", "oodi"});
        neighbours.put("tolo", new String[]{"bole", "berg", "oodi", "bush"});
        neighbours.put("oodi", new String[]{"tolo", "berg", "rich"});
        neighbours.put("rich", new String[]{"oodi", "bush"});
        neighbours.put("bush", new String[]{"tolo", "rich"});

        ArrayList<Node> graph = new ArrayList<>();

        for (String id : nodes.keySet()) {
            nodes.get(id).setId(id);
            
            for (String neighbor : neighbours.get(id)) {
                nodes.get(id).addNeighbour(nodes.get(neighbor));
            }

            graph.add(nodes.get(id));
        }

        return graph;
    }
}

Second arraylist:

public ArrayList<Node> shownodesandlinks() {
    System.out.println("Hello world");
}

All I have managed to do is to make it print "hello world" but I cannot seem to pull the data from the ArrayList into my method. I need to pull the data into my method and then print it in the Main class.

Class I need to print it in:

public class Main {
    public static void main(String[] args) {}
}
1
Can you include a snapshot of both classes in your question? - Henry Twist
What do you mean by "post"? - NomadMaker
exactly what post means, print out :) - Sofelia
"Post" doesn't mean "print", FYI. I've edited your question to say "print" throughout. - John Kugelman
Post and print means pretty much the same thing, atleast to me :) but thanks. Hopefully I will get an answer soon. Been working on this for 6 hours without result - Sofelia

1 Answers

1
votes

Pass the ArrayList returned by createGraph() as an argument to your shownodesandlinks() method.

public void shownodesandlinks(ArrayList<Node> graph) {
    for (Node node : graph) {
        // print node here
    }
}

Your Main class should look like this

public class Main {
    public static void main(String[] args) {
        GraphData data = new GraphData();
        ArrayList<Node> graph = data.createGraph();
        data.shownodesandlinks(graph);
    }
}