Java 如何将包含矩阵的文件读入向量向量?

Java 如何将包含矩阵的文件读入向量向量?,java,Java,我有一个包含以下格式数据的文件: 5 11 24 07 20 03 04 12 25 08 16 17 05 13 21 09 10 18 01 14 22 23 06 19 02 15 3 04 09 02 03 05 07 08 01 06 第一个数字表示正方形矩阵的大小,该数字后面的行是由空格分隔的矩阵元素 如何将这些数据读入向量向量?对不起,我是编程新手。我如何确保一旦程序获得了大小,它就会读取矩阵的精确行数?将有许多矩阵,因此在将值传递给适当的函数后,如何清除向量 我不认为我做的代码

我有一个包含以下格式数据的文件:

5
11 24 07 20 03
04 12 25 08 16
17 05 13 21 09
10 18 01 14 22
23 06 19 02 15
3
04 09 02
03 05 07
08 01 06
第一个数字表示正方形矩阵的大小,该数字后面的行是由空格分隔的矩阵元素

如何将这些数据读入向量向量?对不起,我是编程新手。我如何确保一旦程序获得了大小,它就会读取矩阵的精确行数?将有许多矩阵,因此在将值传递给适当的函数后,如何清除向量

我不认为我做的代码对你有任何意义,但它在这里

File file = new File("magic.txt");
    Scanner sc = new Scanner(file);

    while(sc.hasNextLine())
    {
        String line = sc.nextLine();
        String[] lineArr = line.split(" ");
        int[] elements = new int[lineArr.length];
        int size = sc.nextInt();

        Vector<Vector<Integer>> matrix = new Vector<>(size);
        Vector<Integer> row = new Vector<>(size);

        for(int i = 0; i < size; i++)
        {
            for(int j = 0; j < size; j++)
            {
                row.add(j, elements[i]);
            }
            matrix.add(i, row);
        }

        for(int i = 0; i < size; i++)
        {
            for(int j = 0; j < size; j++)
            {
                System.out.println(matrix.get(i).get(j));
            }
            System.out.println();
        }
    }
File File=new文件(“magic.txt”);
扫描仪sc=新扫描仪(文件);
while(sc.hasNextLine())
{
字符串行=sc.nextLine();
字符串[]lineArr=line.split(“”);
int[]元素=新的int[lineArr.length];
int size=sc.nextInt();
向量矩阵=新向量(大小);
矢量行=新矢量(大小);
对于(int i=0;i
关于您的代码的评论:

  • 您应该使用try with资源

  • 删除
    DataInputStream
    。你甚至没有使用它添加的东西

  • 不要使用
    向量
    ,而是使用
    列表
    。或者构建一个
    int[][]
    ,因为您知道大小

  • 不要在循环之前声明

下面是代码的示例:

try (BufferedReader br = Files.newBufferedReader(Paths.get("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        int size = Integer.parseInt(line);

        List<List<Integer>> matrix = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            if ((line = br.readLine()) == null)
                throw new EOFException();

            List<Integer> row = new ArrayList<>();
            for (String value : line.split(" "))
                row.add(Integer.parseInt(value));
            matrix.add(row);
        }

        System.out.println(matrix);
    }
}
如前所述,您也可以构建二维阵列:

try (BufferedReader br = Files.newBufferedReader(Paths.get("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        int size = Integer.parseInt(line);

        int[][] matrix = new int[size][size];
        for (int i = 0; i < size; i++) {
            if ((line = br.readLine()) == null)
                throw new EOFException();

            String[] values = line.split(" ");
            for (int j = 0; j < size; j++)
                matrix[i][j] = Integer.parseInt(values[j]);
        }

        System.out.println(Arrays.deepToString(matrix));
    }
}
try(BufferedReader br=Files.newBufferedReader(path.get(“file.txt”)){
弦线;
而((line=br.readLine())!=null){
int size=Integer.parseInt(行);
int[][]矩阵=新的int[size][size];
对于(int i=0;i
关于您的代码的评论:

  • 您应该使用try with资源

  • 删除
    DataInputStream
    。你甚至没有使用它添加的东西

  • 不要使用
    向量
    ,而是使用
    列表
    。或者构建一个
    int[][]
    ,因为您知道大小

  • 不要在循环之前声明

下面是代码的示例:

try (BufferedReader br = Files.newBufferedReader(Paths.get("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        int size = Integer.parseInt(line);

        List<List<Integer>> matrix = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            if ((line = br.readLine()) == null)
                throw new EOFException();

            List<Integer> row = new ArrayList<>();
            for (String value : line.split(" "))
                row.add(Integer.parseInt(value));
            matrix.add(row);
        }

        System.out.println(matrix);
    }
}
如前所述,您也可以构建二维阵列:

try (BufferedReader br = Files.newBufferedReader(Paths.get("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        int size = Integer.parseInt(line);

        int[][] matrix = new int[size][size];
        for (int i = 0; i < size; i++) {
            if ((line = br.readLine()) == null)
                throw new EOFException();

            String[] values = line.split(" ");
            for (int j = 0; j < size; j++)
                matrix[i][j] = Integer.parseInt(values[j]);
        }

        System.out.println(Arrays.deepToString(matrix));
    }
}
try(BufferedReader br=Files.newBufferedReader(path.get(“file.txt”)){
弦线;
而((line=br.readLine())!=null){
int size=Integer.parseInt(行);
int[][]矩阵=新的int[size][size];
对于(int i=0;i
这是流API的经典任务

        File file = new File("your-file-path-here");
        try (Scanner scanner = new Scanner(file)) {
            int size = scanner.nextInt();
            List<List<Integer>> result = IntStream.range(0, size)
                    .mapToObj(row ->
                            IntStream.range(0, size)
                                    .mapToObj(i -> scanner.next())
                                    .map(Integer::parseInt)
                                    .collect(Collectors.toList()))
                    .collect(Collectors.toList());
        }
File File=new文件(“此处为您的文件路径”);
尝试(扫描仪=新扫描仪(文件)){
int size=scanner.nextInt();
列表结果=IntStream.range(0,大小)
.mapToObj(行->
IntStream.range(0,大小)
.mapToObj(i->scanner.next())
.map(整数::parseInt)
.collect(收集器.toList())
.collect(Collectors.toList());
}

这是流API的经典任务

        File file = new File("your-file-path-here");
        try (Scanner scanner = new Scanner(file)) {
            int size = scanner.nextInt();
            List<List<Integer>> result = IntStream.range(0, size)
                    .mapToObj(row ->
                            IntStream.range(0, size)
                                    .mapToObj(i -> scanner.next())
                                    .map(Integer::parseInt)
                                    .collect(Collectors.toList()))
                    .collect(Collectors.toList());
        }
File File=new文件(“此处为您的文件路径”);
尝试(扫描仪=新扫描仪(文件)){
int size=scanner.nextInt();
列表结果=IntStream.range(0,大小)
.mapToObj(行->
IntStream.range(0,大小)
.mapToObj(i->scanner.next())
.map(整数::parseInt)
.collect(收集器.toList())
.collect(Collectors.toList());
}

为什么要使用
Vector
?这是一个旧的,大多过时的类。甚至有人这样说:如果不需要线程安全实现,建议使用
ArrayList
代替
Vector
。为什么要使用
DataInputStream
?你甚至没有使用它添加的任何东西。因为我的教授,我不得不使用向量。他还不允许我们使用任何其他数据结构:(是的,我刚刚意识到我可以使用扫描仪进行输入。显然,他遇到了James Gosling,并在Gosling发布Java时与他谈论了Java。所以他已经很老了哈哈。欢迎这么说!当你提出问题时,试着添加一个最小的内容:输入样本、预期输出样本、你尝试了什么、研究了什么以及你在哪里堆积。它在询问之前,您似乎没有做过任何研究。为什么要使用
Vector
?这是一个旧的、大部分已经过时的类。甚至有人这样说:如果不需要线程安全的实现,建议使用
ArrayList
来代替
Vector
。为什么要使用
DataInputStream