如何使用方法在Main.java中打印ArrayList?

如何使用方法在Main.java中打印ArrayList?,java,class,arraylist,methods,Java,Class,Arraylist,Methods,我被要求制作一个打印ArrayList的方法。我如何在不同的类中创建一个方法,然后从该类中提取数据以在Main类中打印它 例如: 使用数据初始化: public class GraphData { public ArrayList<Node> createGraph() { LinkedHashMap<String,Node> nodes = new LinkedHashMap<>(); nodes.put("

我被要求制作一个打印ArrayList的方法。我如何在不同的类中创建一个方法,然后从该类中提取数据以在
Main
类中打印它

例如:

使用数据初始化:

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;
    }
}
public ArrayList<Node> shownodesandlinks() {
    System.out.println("Hello world");
}

createGraph()
返回的
ArrayList
作为参数传递给
showNodesAndLink()
方法

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

你能在你的问题中包括这两个类的快照吗?你说的“post”是什么意思?post的确切含义是什么,打印出来:)“post”不是“print”,仅供参考。我把你的问题改成了“打印”。至少对我来说,Post和print的意思差不多:)不过谢谢。希望我能很快得到答复。我已经做了6个小时没有结果了谢谢!这种方法奏效了。问题是我将其作为一个输出:“at ruttsokning.Node.addneighbor(Node.java:76)”而不是实际的输出。
public void shownodesandlinks(ArrayList<Node> graph) {
    for (Node node : graph) {
        // print node here
    }
}
public class Main {
    public static void main(String[] args) {
        GraphData data = new GraphData();
        ArrayList<Node> graph = data.createGraph();
        data.shownodesandlinks(graph);
    }
}