简体   繁体   中英

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.

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,

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

I declared a two dimensional array called graph2 in order to hold the values that came from the lists. But the problem is I cannot find the relationship to populate data from values list to fit in the graph2 2d array. 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 ). 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)

nodes will only contain the special nodes and graph2 will contains the edges descriptors.

(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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