2016-12-15 71 views
-1

我編輯此代碼,因爲我不能永久刪除它。我編輯了這段代碼,因爲我不能永久刪除它。我編輯了這段代碼,因爲我不能永久刪除它。我編輯的代碼,因爲我不能永久刪除 。我編輯的代碼,因爲我不能刪除它永久這個標題已被編輯

請不要重新編輯

+0

線索:看看這兩種方法有什麼共同的代碼,將這些代碼提取到一個新的方法並從兩種方法中調用它。 – radoh

回答

1

你應該從創建文件的加載分離成圖的圖。

事情是這樣的:

public interface GraphLoader { 
    /** 
    * Fill a graph with no property from a single file. 
    * 
    * @param graph the graph to fill 
    * @param path path to graph file 
    * @return a graph with no property 
    */ 
    public static void loadSingleFile(Graph graph, Path path) { 
     try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path.toFile()))) { 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       String[] vertices = line.split(" "); 
       if (vertices.length < 2) { 
        continue; 
       } 
       graph.addEdge(Integer.parseInt(vertices[0]), Integer.parseInt(vertices[1])); 
      } 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * load a graph with no property from a single file. 
    * 
    * @param path path to graph file 
    * @return a graph with no property 
    */ 
    public static Graph loadSingleFile(Path path) { 
     Graph graph = new Graph(); 
     loadSingleFile(graph, path); 
     return graph; 
    } 

    /** 
    * load a graph with no property from multiple files in parallel. 
    * 
    * @param paths paths to graph files 
    * @return a graph with no property 
    */ 
    static Graph loadMultipleFiles(Path[] paths) { 
     Graph graph = new Graph(); 
     for (Path path : paths) { 
      loadSingleFile(graph, path); 
     } 
     return graph; 
    } 

} 
+0

非常感謝你的回答,這是非常有幫助的,我希望我可以讓你的聯繫方式問你更多的問題,如果這不是太多問 – questionner

+0

@benimane - 對不起 - 沒有個人接觸SO - 只是發表另一個問題和有人會幫忙。 – OldCurmudgeon

+0

我發佈了另一個問題,我很感激你是否可以幫助我「http://stackoverflow.com/questions/41220825/similar-methods-with-a-slight-difference-can-i-merge-java#」 – questionner