2012-12-06 27 views
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個conections

0; 1; 500; //節點0與500 kbps的

1連接到節點1; 2; 500 //節點1連接到節點2 500 kbps的

當節點1被連接到節點2,它實際上還連接到節點0也是如此。這是否能夠把它放在一個hashmap?

我對此有點困惑。在此先感謝您的幫助。

+0

你能舉一個例子說明文件的外觀如何,數據代表什麼以及如何讓它在一個有向圖中結束?我們並不知道你想要做什麼。 – jbx

+0

嗨,我很抱歉,我已經遺漏了我的數據txt文件。我已經把我將在上面閱讀的示例txt文件。謝謝 – Eric

回答

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

這種方法適用於有向圖,假設你想從節點之一的箭頭方向穿過。當然還有其他的選擇(例如將其存儲爲大的2D矩陣,其中kbps作爲矩陣中的數字,左邊的節點數字表示'from'節點,並且頂部的節點數量'to'節點或其他方式)。

+0

非常感謝..解釋非常詳細..謝謝你的幫助..謝謝.. – Eric