Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HashMap的HashMap_Java - Fatal编程技术网

Java HashMap的HashMap

Java HashMap的HashMap,java,Java,我有以下内容: 氢化锂:Li1H1#评论 我希望得到以下结果: {氢化锂':{'Li':1,'H':1},…} 我这样做: public static HashMap<String,HashMap<String,Integer>> readFormulas(String content){ HashMap<String,Integer> dictionnaire1 = new HashMap<String,Integer>();

我有以下内容:

氢化锂:Li1H1#评论

我希望得到以下结果:

{氢化锂':{'Li':1,'H':1},…}

我这样做:

public static HashMap<String,HashMap<String,Integer>> readFormulas(String content){
    HashMap<String,Integer> dictionnaire1 = new HashMap<String,Integer>();
        HashMap<String,HashMap<String,Integer>> dictionnaire2 = new HashMap<String,HashMap<String,Integer>>();
        ArrayList <String>  al = new ArrayList<String>();
        al.add(Arrays.toString(content.split(":")));// on met chaque ligne dans un tableau
        String str[] = content.split("\n");
        for(int i = 0 ; i < str.length;i++){//pour chaque ligne 
            String str2 [] = str[i].split(":");// séparer les noms des molécules des noms scientiques et des  commentaires ["Helium","he 1 #fzefzezfezfz"
            String NomMolecule = str2[0];
            String diese [] = str2[1].split("#");
            String description [] = diese[0].split(" ");
            int nb = Integer.parseInt(description[2]);
            dictionnaire1.put(description[1], nb);
            }
            dictionnaire2.put(NomMolecule,dictionnaire1);
        }
      return(dictionnaire2);
     }
publicstatichashmap读取公式(字符串内容){
HashMap dictionnaire1=新HashMap();
HashMap dictionnaire2=新HashMap();
ArrayList al=新的ArrayList();
al.add(Arrays.toString(content.split(“:”);//在met chaque LINGE dans un TABLEU上
字符串str[]=content.split(“\n”);
对于(int i=0;i
但结果很糟糕,我不明白为什么??: 参赛作品:

 HashMap<String,HashMap<String,Integer>> formulas = readFormulas("helium : he 3 #fsdfsfsdfsf" + "\n" + "lithium hydride : Li 1 H 1 #fdvdfdfvd");
HashMap formulas=readFormulas(“氦:he 3#fsdfsf”+“\n”+“氢化锂:Li 1 H 1#fdvdfdfvd”);
结果:

{氢化锂={he=3,Li=1},氦={he=3,Li=1}


每个分子需要一个HashMap:

for(int i = 0 ; i < str.length;i++){//pour chaque ligne 
    String str2 [] = str[i].split(":");
    String NomMolecule = str2[0];
    String diese [] = str2[1].split("#");
    String description [] = diese[0].trim().split(" ");
    HashMap<String,Integer> dictionnaire1 = new HashMap<>();
    for( int j = 0; j < description.length; j += 2 ){
        int nb = Integer.parseInt(description[j+1]);
        dictionnaire1.put(description[j], nb);
    }
    dictionnaire2.put(NomMolecule,dictionnaire1);
}
for(int i=0;i
我已经添加了一个分子的成分(例如Li 1 H 1),但你也缺少了一个循环。

非常感谢劳恩:)。但我不明白这一点:为什么j+=2一个代表“Li”,一个代表“1”就等于2。然后,一个代表“H”,一个代表“1”就等于2。
HashMap<String,Integer> dictionnaire1 = new HashMap<String,Integer>();
for(int j=1;j<description.length-1;j+=2){
    int nb = Integer.parseInt(description[j+1]);
    dictionnaire1.put(description[j], nb);
}