0

我有读取文件功能,它将读取一个 txt 文件。读完后,我将值放入列表中。以下是样本数据:

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());
}
}

我的 txt 数据文件将类似于:

2// 共 2 个连接

0;1;500; // 节点 0 以 500 kbps 连接到节点 1

1;2;500 // 节点 1 以 500 kbps 连接到节点 2

当节点 1 连接到节点 2 时,它实际上也连接到节点 0。这能把它放在哈希图上吗?

我对此有点困惑。提前感谢您的帮助。

4

1 回答 1

1

有多种方法可以做到这一点。由于每条边都有一个速度,你可以为每个节点设置一个类,为每条边设置一个类:

创建一个代表您的节点的类。它应该携带数据(节点 id),以及它从它发出的连接(边)(因为它是有向图)。

public class Node
{
  private int nodeId;
  private List<Connection> outboundConnections = new ArrayList<>();

  public Node(int nodeId)
  {
    this.nodeId = nodeId;
  }

  public void addConnection(Connection connection)
  {
    this.outboundConnections.add(connection);
  }

  //... setters and getters
}

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):

   public class Connection
   {
      private int speedKbps;
      private Node endNode;

      public Connection(Node endNode, int speedKbps)
      {
        this.endNode = endNode;
        this.speedKbps = speedKbps;
      }

      //... setters and getters
   }

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

Map<Integer, Node> nodes = new TreeMap<>(); 

Then for each line in your loop you can do:

int fromNodeId = new Integer(values[0]);
int toNodeId = new Integer(values[1]);
int speedKbps = new Integer(values[2]);

Node fromNode = nodes.get(fromNodeId);
if (fromNode == null) //if we haven't seen this node already, create it and add it to the map
{
   fromNode = new Node(fromNodeId);
   nodes.put(fromNodeId, fromNode);
}

Node toNode = nodes.get(toNodeId);
if (toNode == null) //if we haven't seen this node already, create it and add it to the map
{
   toNode = new Node(toNodeId);
   nodes.put(fromNodeId, toNode);
}

Connection connection = new Connection(toNode, speedKbps);
fromNode.addConnection(connection);

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

于 2012-12-06T15:59:55.983 回答