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は500kbpsでノード1に接続します

1; 2;500//ノード1は500kbpsでノード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
}

次に、接続のデータと接続先のノード (有向グラフであるため、宛先) を含むエッジを表すクラスを作成します。

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

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

      //... setters and getters
   }

クラスでは、作成されたすべてのノードのマップを保持します (それがクラスのメンバーである場合に最適ですが、何をしているかによって異なります)。

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

次に、ループ内の各行に対して次のことができます。

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

このアプローチは、ノードの 1 つから矢印の方向にトラバースすることを前提として、有向グラフで機能します。

もちろん、他の選択肢もあります (たとえば、kbps をマトリックス内の数値として、左側のノード番号が「from」ノードを表し、上部のノード番号が「to」ノードを表す大きな 2D マトリックスとして格納するなど)。ノード、またはその逆)。

于 2012-12-06T15:59:55.983 に答える