2012-12-06 4 views
0

나는 txt 파일을 읽을 파일 읽기 기능이 있습니다. 내가 그것을 읽은 후에, 저는 그 값을 목록에 넣었습니다.readfile 및지도에지도

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 데이터 파일이있을 것입니다 뭔가 같은 : 아래의 샘플 데이터이며, 1 500

2 // 총 2 conections

0; // 노드 0 500 kbps의

1 노드 (1)에 연결하고 2; 500 // 노드 1 2 500 kbps의 노드 (1) 노드 (2)에 접속 될 때 가

, 실제로도 연결된다

를 노드에 연결 노드 0도 마찬가지입니다. 이 해시 맵에 넣을 수 있습니까 ??

나는 약간 혼란 스럽다. 도와 주셔서 미리 감사드립니다.

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

.

물론 다른 대안이 있습니다 (예 : 행렬에 숫자로 kbps, '보낸 사람'노드를 나타내는 왼쪽에 노드 번호, 맨 위에 노드 번호가있는 대형 2D 매트릭스로 저장) 'to'노드 또는 다른 방향 라운드).

+0

고맙습니다 .. 설명은 매우 세부 .. 당신의 도움에 감사드립니다 .. .. 감사합니다 .. Logged – Eric

관련 문제