【发布时间】:2021-06-12 07:17:48
【问题描述】:
我被要求制作一个打印 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");
}
我所做的只是让它打印“hello world”,但我似乎无法将 ArrayList 中的数据提取到我的方法中。我需要将数据拉入我的方法中,然后在Main 类中打印。
我需要打印的班级:
public class Main {
public static void main(String[] args) {}
}
【问题讨论】:
-
你能在你的问题中包含两个类的快照吗?
-
“发布”是什么意思?
-
post 到底是什么意思,打印出来:)
-
“发布”并不意味着“打印”,仅供参考。我已将您的问题编辑为始终说“打印”。
-
发布和打印意味着几乎相同的东西,至少对我来说 :) 但谢谢。希望我能尽快得到答复。已经为此工作了 6 个小时,但没有结果
标签: java class arraylist methods