2014-12-03 22 views
0

,我要開始創建
類文件讀取一個文件,但我有分隔符的問題:[,] 類頂點attribut INT NUM;鄰居列表 類鄰居attribut INT vertexnum;鄰居下一個; 我怎麼能說,0是一個頂點,其鄰居頂點是2和9 輸入文件1: 0:[2,9] 1:[8,5,7,2] ..... 我是初學者,我怎麼讀這個文件,0是一個頂點,其鄰居是2和9,其他頂點相同文件讀取,頂點和adjlist

我想用鄰接表0表示鄰接表2和9的鄰接表... .. 請幫我

回答

0

可能是像(不是很強大/彈性和未測試),只是想知道如何做到這一點。

我認爲該文件是格式

0: [2,3,4] 
1: [2,4,6] 
etc 

Java代碼:

FileInputStream fis = new FileInputStream(fin); 

//Construct BufferedReader from InputStreamReader 
BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 

String line = null; 
while ((line = br.readLine()) != null) { 
     int vertex = Integer.parseInt(line.substring(0, line.indexOf(":")) //get the number before : 
     String neighboorString = (line.substring(line.indexOf("[")+1, line.indexOf("]") //get the string between [ ] 
     List<Integer> neighbors = new ArrayList<Integer>(); 
     for (String neighbor : neighborString.split(",")) { //split the string between , to get the neighbor id 
      neighbors.add(Integer.parseInt(neighbor)) 

     } 
     //populate your structure as required) 
} 
//close the streams/readers 
+0

非常感謝你的代碼非常有幫助 – etudiant 2014-12-03 17:16:35

+0

請我想要的顏色此圖有5種顏色爲了這個,我問2條件,但我不知道我是如何在Java的初學者幫助我 - 如果一個圖的頂點有4個鄰居,它可以使用其鄰居未使用的顏色對頂點進行着色 - 如果頂點可能有5個鄰居爲頂點着色顏色不被其鄰居使用 請如何閱讀該文件,因爲我在頁面頂部指示要應用顏色條件,返回每種顏色的頂點列表請幫助我 – etudiant 2014-12-05 22:47:58