i have read file function which will read a txt file. after i read it, i put the value to a list. below is sample data:
public void readDisplayClient()
{
DisplayClient dc = null;
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("ClientData.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String [] values = new String[3];
int counter = 0;
int clientCounter = 0;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
String delims = ";";
String[] tokens = strLine.split(delims);
if(counter == 0)//Reading total clients
{
totalClient = Integer.parseInt(tokens[0]);
counter++;
}
else
{
//System.out.println("Test " + counter + " " + tokens.length);
for (int i = 0; i < tokens.length; i++)
{
values[i] = tokens[i];
//System.out.println(tokens[i]);
}
dc = new DisplayClient(clientCounter,values[0],values[1],values[2]);
//dc.printDetails(); // save the connected nodes details to logger txt file.
clientList.add(dc);
clientCounter++;
}
}
//Close the input stream
in.close();
ss.setTotalClient(totalClient);
ss.setClientList(clientList);
//ss.printClientList();
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
my txt data file will be something like :
2// total 2 conections
0;1;500; // Node 0 connect to Node 1 with 500 kbps
1;2;500 // Node 1 connect to Node 2 with 500 kbps
when node 1 is connected to node 2, it is actually also connect to node 0 as well. Is this able to put it on a hashmap ??
i am a bit confused on it. thanks in advance for help.
There are various ways to do it. Since each edge has a speed you can have a class for each node and a class for each edge:
Create a class which represents your node. It should carry the data (node id), and which connections (edges) it has going out from it (since its a directed graph).
Then create a class which represents the edges, including the data for the connection and which node it connects to (destination, since its a directed graph):
In your class you keep a Map of all the created nodes (best if it is a member of the class but depends on what you’re doing).
Then for each line in your loop you can do:
This approach works for a directed graph, assuming you want to traverse from the one of the nodes in the direction of the arrows.
There are of course other alternatives (for example storing it as a large 2D matrix, with the kbps as the number in the matrix and the node numbers on the left representing ‘from’ nodes, and the node numbers on the top the ‘to’ nodes, or the other way round).