简体   繁体   English

从列表中填充2d数组

[英]Filling 2d Array from lists

I have a file which contains the following data : 我有一个包含以下数据的文件:

4
5
0 2

0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3

the first line is the nodes number, the second line is edges number and the 3rd line contains the nodes with special constraints. 第一行是节点号,第二行是边数,第三行包含具有特殊约束的节点。

However, I used this code to read it from the file into two lists,values and nodes, where values list contain the edges ( for example: 0 1 0.6), while the nodes list contain the values of the 3 lines ( 4 , 5, {0,2} ) respectively. 但是,我使用此代码将其从文件读取到两个列表,值和节点,其中值列表包含边(例如:0 1 0.6),而节点列表包含3行的值(4,5 ,{0,2})。

I need to construct two arrays one is an equivalent to adjacency matrix which is like this 我需要构造两个数组,一个等价于邻接矩阵,就像这样

int graph[][] = {
            {0, 6, 2, 5},
            {6, 0, 0, 8},
            {2, 0, 0, 3},
            {5, 8, 3, 0}
        };

and the other array is the special nodes id, ie, 另一个数组是特殊节点id,即

int  special [] ={0,2};

I can read the values from a file and separate them into two lists nodes and values in the following shape: nodes list contains : 4 ,5 ,0 ,2 values list contains : 0,1,0.6,0,2,0.2,0,3,0.5,1,3,0.8,2,3,0.3 我可以从文件中读取值并将它们分成两个列表节点和值,形式如下:节点列表包含:4,5,0,2值列表包含:0,1,0.6,0,2,0.2,0 ,3,0.5,1,3,0.8,2,3,0.3

I declared a two dimensional array called graph2 in order to hold the values that came from the lists. 我声明了一个名为graph2的二维数组,以保存来自列表的值。 But the problem is I cannot find the relationship to populate data from values list to fit in the graph2 2d array. 但问题是我无法找到从值列表中填充数据以适应graph2 2d数组的关系。 It must to be like the graph array but its initialization would be dynamic from that file. 它必须像图形数组,但它的初始化将是该文件的动态。

So, basically what I need is to create two arrays: one is like the graph array( 2d array) and the other one is like special array( 1d ). 所以,基本上我需要创建两个数组:一个是图形数组(2d数组),另一个类似于特殊数组(1d)。 Based on the file I must do that. 根据文件,我必须这样做。 Thanks in advance 提前致谢

My code: 我的代码:

List<Double> values = new ArrayList<>();
        List<Integer> nodes = new ArrayList<>();
        int c = 0;
        File file = new File("text.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                if (c <= 2) {

                    String[] str = text.split(" ");
                    for (int i = 0; i < str.length; i++) {
                        if (str[i].trim().length() > 0) {
                            nodes.add(Integer.parseInt(str[i]));
                        }
                    }
                    c++;
                } else {

                    String[] str = text.split(" ");
                    for (int i = 0; i < str.length; i++) {
                        if (str[i].trim().length() > 0) {
                            values.add(Double.parseDouble(str[i]));
                        }
                    }
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                System.out.print(e);
            }
        }
        double graph2 [] []  =new double [nodes.get(0)][nodes.get(0)]; 

The following code will create the 2D array from your file (not the adjacency map that you can then easily create yourself) 以下代码将从您的文件创建2D数组(而不是您可以自己轻松创建的邻接映射)

nodes will only contain the special nodes and graph2 will contains the edges descriptors. nodes将只包含特殊节点,graph2将包含边缘描述符。

(NB: I did not test it so tell me if something goes wrong) (注意:我没有测试过,所以告诉我是否出了问题)

List<Double> values = new ArrayList<>();
List<Integer> nodes = new ArrayList<>();
int c = 0;
File file = new File("text.txt");
BufferedReader reader = null;

double[][] graph2 = null;    

try {
    reader = new BufferedReader(new FileReader(file));
    String text = null;
    while ((text = reader.readLine()) != null) {
        // The first line gives the number of nodes (You will use to create the int[][] graph = new int[nOfNodes][nOfNodes];)
        if (c == 0) {
            numberOfNodes = Integer.parseInt(text.trim());
        }
        // The second one gives the number of edges
        else if (c == 1) {
            nOfEdges = Integer.parseInt(text.trim());
            graph2 = new double[nOfEdges][3];
        }
        // And third the list of special nodes
        // `nodes` will now contains only your special constrained one
        else if (c == 2) {
            String[] str = text.split(" ");
            for (int i = 0; i < str.length; i++) {
                if (str[i].trim().length() > 0) {
                    nodes.add(Integer.parseInt(str[i]));
                }
            }
        } else { // Then you have your edges descriptors
            String[] str = text.split(" ");
            for (int i = 0; i < str.length; i++) {
                if (str[i].trim().length() > 0) {
                     graph2[c-4][i] = Double.parseDouble(str[i]);                     
                }
            }
        }
        c++;
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        System.out.print(e);
    }
}

Then to create your graph you can initialize it to zeros and then loop over graph2 to populate it. 然后,要创建graph您可以将其初始化为零,然后循环遍历graph2以填充它。

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

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