2017-04-18 49 views
-2

在这段代码中,我试图在第一个代码中使用hashmap.so创建一个kruskal算法代码的实现代码,我将代码保存到一个字符串中,然后将字符串的值输入到散列表中。我有一个字符串中的数字。如何把数字放到hashmap中?

private static final String FILENAME = "D:\\Kuliah\\Semester 7\\~USUL BULAN JANUARI\\PROBLEM\\sepuluh\\1.dat"; 

    public static void main(String[] args) { 

       String content = ""; 

     BufferedReader br = null; 
     FileReader fr = null; 

     try { 

      fr = new FileReader(FILENAME); 
      br = new BufferedReader(fr); 

      String sCurrentLine; 

      br = new BufferedReader(new FileReader(FILENAME)); 

      while ((sCurrentLine = br.readLine()) != null) { 
          content = content + sCurrentLine + "\n"; 
    //   System.out.println(sCurrentLine); 
      } 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } finally { 

      try { 

       if (br != null) 
        br.close(); 

       if (fr != null) 
        fr.close(); 

      } catch (IOException ex) { 

       ex.printStackTrace(); 

      } 

     } 

      System.out.print(content); 

    } 
     public static String extractNode(String content){ 
      String[] lines = content.split("\n"); 
      HashMap<Integer, Node> vertexs = new HashMap<Integer, Node>(); 

      for(int i=0;i<lines.length;i++){ 
      Node node = new Node(); 
      node.setId(0); 
      vertexs.put(0, node); 

       //at this code how can i put it? 

      } 





      return ""; 
     } 




} 
+2

您是否尝试过搜索“java String to integer”? –

+0

有必要吗?因为我认为字符串的值可以直接输入到hashmap。在我的散列表中没有需要的整数值,但它是ID。 –

+0

你有'HashMap ',所以是的,有必要遵循这些对象类型。如果你想要hashmap保存一个字符串,然后做一个'HashMap ' –

回答

0

例如,你可以不实现这个:

String x = "5"; 
int num = Integer.parseInt(x); //coverts String to Integer 

希望这有助于!

`

+0

我认为字符串的值可以直接输入到hashmap,尽管字符串中的值是整数。但在我的散列表整数只是为了uniqe数字而不是值。 –

相关问题