你可能想要做的是创建一个节点类,其中有一个字段指示该节点能够连接到哪个节点。然后您需要从文件中读取连接,并使用Node构造函数中的数据建立连接。
关于读取数据,您可以使用ArrayList,Array和Buffered Reader来完成此操作。它首先设置ArrayList,然后使用BufferedReader获取连接,将每个对存储在数组中并将其存储在ArrayList中以供引用。
//ArrayList which will hold the arrays containing the connections
ArrayList<int[]> connections = new ArrayList<int[]>(4);
Path pathway = Paths.get("D:/Users/mgreenma/Desktop/tester.txt");//Path to file
//Using a BufferedReader and FileReader we access the file
try(BufferedReader reader = new BufferedReader(new FileReader(pathway.toFile()))){
String line = "";
Scanner sc = new Scanner(line);
while((line = reader.readLine()) != null){ //Read each line until there are no more
sc = new Scanner(line); //Set up a scanner
int[] conn = new int[2]; //An array to hold each pair of nodes
conn[0] = sc.nextInt(); //Get first node
conn[1] = sc.nextInt(); //And the second
connections.add(conn); //Add to the ArrayList
}
sc.close(); //Close scanner
} catch (IOException io){
System.out.println("Error: " + io.getMessage());
}
for(int[] i : connections){//Print our connections list
System.out.println("Connection: " + i[0] + "," + i[1]);
}
祝你好运!
我们无法为您编写程序。你有现有的代码,我们可以看看你有问题吗? – BradleyDotNET