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, i.e.,
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)];