Java 如何将.txt文件中的数据保存为数组

Java 如何将.txt文件中的数据保存为数组,java,Java,我正试图编写一个程序,读取网络中相互作用的节点列表。它以以下格式写入文本文件: node1 node2 node1 node3 node2 node3 node3 node5 node2 node1 如果我输入节点的名称,程序将能够读取该文件并删除任何重复的交互,并且能够向我返回一个节点与其他节点的交互次数。我是Java的完全初学者,虽然我已设法将其读入文件(见下文),但我不知道如何将此数据保存为数组,以便调用单个节点(如node1),并返回其连接到的节点数以及这些节点是什

我正试图编写一个程序,读取网络中相互作用的节点列表。它以以下格式写入文本文件:

node1   node2
node1   node3
node2   node3
node3   node5
node2   node1
如果我输入节点的名称,程序将能够读取该文件并删除任何重复的交互,并且能够向我返回一个节点与其他节点的交互次数。我是Java的完全初学者,虽然我已设法将其读入文件(见下文),但我不知道如何将此数据保存为数组,以便调用单个节点(如node1),并返回其连接到的节点数以及这些节点是什么的整数值:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class ReadFile {
    
    public static void main(String[] args) throws FileNotFoundException {
        
        File my_file = new File("(Pathway to file)/nodes.txt");
        Scanner my_scanner = new Scanner(my_file);
        
        while(my_scanner.hasNextLine()) {
            System.out.println(my_scanner.nextLine());
        }
    }
}
这将输出所需的数据,但不会将其保存为数组。
任何帮助都会很好,谢谢

将节点添加到列表中,并将其转换为数组,例如:

public static void main(String[] args) throws FileNotFoundException {
    
    File my_file = new File("(Pathway to file)/nodes.txt");
    Scanner my_scanner = new Scanner(my_file);
    String[] myArray;
    ArrayList<String> list = new ArrayList<>();
    
    while(my_scanner.hasNextLine()) {
        String line = my_scanner.nextLine();
        for (String word : line.split(" +")) {
          list.add(word);
        }
        System.out.println(line);
    }
    myArray = list.toArray();
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
File my_File=新文件(“(文件路径)/nodes.txt”);
扫描仪my_Scanner=新扫描仪(my_文件);
字符串[]myArray;
ArrayList=新建ArrayList();
while(my_scanner.hasNextLine()){
String line=my_scanner.nextLine();
for(字符串字:line.split(“+”)){
列表。添加(word);
}
系统输出打印项次(行);
}
myArray=list.toArray();
}

我建议将文件格式更改为:

1 2
1 3
2 3
3 5
2 1
因为其余部分没有添加任何信息。如果您无法控制文件格式,那么还有其他工作区(您可以研究使用正则表达式)

您可以使用集合的ArrayList来表示已发送的连接,这样就不会有重复的连接。如果您确实需要一个数组,您可以稍后使用ArrayList的
toArray()
方法对其进行转换

如果您按照建议更改文本文件的格式,下面的代码应该完成您描述的每件事,并打印每个节点,然后打印它连接到的所有节点:

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class DavidSmith {
    private static ArrayList<HashSet<Integer>> connections;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner my_scanner = new Scanner(DavidSmith.class.getClassLoader().getResourceAsStream("(Pathway to file)/nodes.txt"));
        connections = new ArrayList<>();
        while(my_scanner.hasNextInt()) {
            int node1 = my_scanner.nextInt(), node2 = my_scanner.nextInt();
            while (connections.size() <= Math.max(node1, node2)) connections.add(null);
            if (connections.get(node1) == null) {
                HashSet<Integer> set = new HashSet<>();
                set.add(node2);
                connections.set(node1,set);
            } else {
                connections.get(node1).add(node2);
            }
            if (connections.get(node2) == null) {
                HashSet<Integer> set = new HashSet<>();
                set.add(node1);
                connections.set(node2,set);
            } else {
                connections.get(node2).add(node1);
            }
        }
        printConnections();
    }
    public static void printConnections(int node) {
        for (int node = 0; node<connections.size(); node++) {
            if (connections.get(node) == null) continue;
            System.out.print("node" + node + " is connected to: ");
            for (int connection : connections.get(node)) System.out.print("node" + connection + " ");
            System.out.println();
        }
    }
}

node1 is connected to: node2 node3 
node2 is connected to: node1 node3 
node3 is connected to: node1 node2 node5 
node5 is connected to: node3