繁体   English   中英

如何从文本文件读取并将数据放入带有3个参数的2D数组中的Java?

[英]How to read from text file and put data into 2D array with 3 parameters in java?

我有一个文本文件,数据存储如下:

7,12
0,1,2
0,4,4
0,5,1
1,0,2
1,2,4
1,3,1
1,6,2
2,1,4
2,3,2
3,1,1
3,2,2
3,4,6
3,6,4
4,0,4
4,3,6
4,5,7
4,6,3
5,0,1
5,4,7
5,6,3
6,1,2
6,3,4
6,4,3
6,5,3

第一行讲述顶点的数量和边的数量,其他行讲述两个顶点和该边缘的权重。 我不知道如何将除第一行以外的所有行存储在2D数组中,如下所示:

int[][] edges = 
    {
        {0,1,2}, {0,4,4}, {0,5,1}, {1,0,2}, {1,2,4}, {1,3,1}, {1,6,2}, {2,1,4},
        {2,3,2}, {3,1,1}, {3,2,2}, {3,4,6}, {3,6,4}, {4,0,4}, {4,3,6}, {4,5,7},
        {4,6,3}, {5,0,1}, {5,4,7}, {5,6,3}, {6,1,2}, {6,3,4}, {6,4,3}, {6,5,3}
    };

请帮忙!

您可以使用列表,然后填充一个int 2D矩阵。 代码很脏,需要重构,但是您的edges将具有文件graph.txt的值

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        int a, b;
        int c, d, e;
        int[][] edges;
        ArrayList<ArrayList<Integer>> intlist = new ArrayList<ArrayList<Integer>>();
        int count = 0;
        try (BufferedReader br = new BufferedReader(new FileReader("/home/dac/gs-rest-service/gnu/src/main/java/gnu/graph.txt"))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            String[] lineVector;

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
                if (line == null)
                    break;

                String[] lineVector2;
                lineVector2 = line.split(",");

                //parsing the values to Integer
                c = Integer.parseInt(lineVector2[0]);
                d = Integer.parseInt(lineVector2[1]);
                e = Integer.parseInt(lineVector2[2]);

                int[] array = new int[3];

                array[0] = c;
                array[1] = d;
                array[2] = e;

                List<Integer> intList2 = new ArrayList<Integer>();
                for (int index = 0; index < array.length; index++) {
                    intList2.add(array[index]);
                }
                intlist.add(new ArrayList<Integer>(intList2));
                count++;

            }
            //System.out.println(intlist);
            int[] list;
            int index = 0;
            edges = new int[intlist.size()][count];
            for (ArrayList<Integer> b2 : intlist) {
                //necessary code here
                list = toIntArray(b2);
                edges[index] = list;
                index++;
            }
            System.out.println("edges:");
            for (int i = 0; i < edges.length; i++) {
                for (int j = 0; j < edges[i].length; j++) {
                    System.out.print(edges[i][j] + " ");
                }
                System.out.println();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }


    }

    static int[] toIntArray(List<Integer> list) {
        int[] ret = new int[list.size()];
        for (int i = 0; i < ret.length; i++)
            ret[i] = list.get(i);
        return ret;
    }
}

输出显示edges变量

edges:
0 1 2 
0 4 4 
0 5 1 
1 0 2 
1 2 4 
1 3 1 
1 6 2 
2 1 4 
2 3 2 
3 1 1 
3 2 2 
3 4 6 
3 6 4 
4 0 4 
4 3 6 
4 5 7 
4 6 3 
5 0 1 
5 4 7 
5 6 3 
6 1 2 
6 3 4 
6 4 3 
6 5 3 

因为您没有发布任何代码,所以我假设您不知道该如何完成,但是不要期望我收到任何高质量的代码。 以下代码是我将尝试解决的方法。 它不是最大的解决方案,但可以完成工作。

try {
            InputStream is = new FileInputStream("file");
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            int[][] edges = new int[24][3];
            String line = "";
            int count = 0;
            int countpos = -1;
            while ((line = br.readLine()) != null) {
                count++;
                if(count!=1) {
                    countpos++;
                    String splitValue = line.split(",")[0];
                    String splitValue2 = line.split(",")[1];
                    String splitValue3 = line.split(",")[2];
                    edges[countpos][0] = Integer.valueOf(splitValue);
                    edges[countpos][1] = Integer.valueOf(splitValue2);
                    edges[countpos][2] = Integer.valueOf(splitValue3);

                }
            }
            System.out.println("Value read: " + countpos );
            for(int i = 0; i < edges.length; i++){
                System.out.print(edges[i][0]);
                System.out.print(edges[i][1]);
                System.out.println(edges[i][2]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM