简体   繁体   English

通过多维数组列表迭代到Java中的hashMap

[英]Iterate through multidimensional arraylist to hashMap in java

I have a table as follows 我有一张桌子,如下

                     Total  Upright  Supine  Meal  PostPr  ChestP     Other
Duration of Period   22:38  14:30   08:08  01:02   06:54   00:48     00:28
Number of Refluxes    5       4       1      0       3       2         0

From this I have created a multidimensional array Arr_ReflDay1_table2d as follows: 由此,我创建了一个多维数组Arr_ReflDay1_table2d ,如下所示:

 [[Total, Upright, Supine, Meal, PostPr, ChestP, Other], [Duration of Period , 22:38, 14:30, 08:08, 01:02, 06:54, 00:48, 00:28], [Number of Refluxes, 5, 4, 1, 0, 3, 2, 0]]

I want to get it into a HashMap such that the Key is the row and column name a value came from. 我想将其放入HashMap中,以使Key是值的行和列名称。 For example the hashMap might read: 例如,hashMap可能显示为:

Total_DurationOfPeriod=22:38,Upright_DurationOfPeriod=14:30,Supine_DurationOfPeriod=08:08 etc. Total_DurationOfPeriod=22:38,Upright_DurationOfPeriod=14:30,Supine_DurationOfPeriod=08:08

I tried doing this: 我尝试这样做:

Map<String,String> mapReflDay1= new LinkedHashMap<String,String>();
    ArrayList<List<String>> Arr_ReflDay1_table2d = new ArrayList<List<String>>();
        for (int ff=0;ff<Arr_ReflDay1_table2d.size();ff++){
               for (int fg=0;fg<Arr_ReflDay1_table2d.size();fg++){
                  for (int fx=0;fx<Arr_ReflDay1_table2d.get(0).size();fx++){
                            mapReflDay1.put("ReflDay1"+Arr_ReflDay1_table2d.get(0).get(ff)+Arr_ReflDay1_table2d.get(fg).get(0)+","Arr_ReflDay1_table2d.get(ff).get(fx));
        }
                }
                      } 

which gives the key fine but the value seems to only be taken from the final column 这样可以很好地设置密钥,但该值似乎仅取自最后一列

Solved it in the end. 最终解决了。 Just added an extra element to the first inner ArrayList so I was iterating over the same number of elements in each inner ArrayList 刚刚在第一个内部ArrayList中添加了一个额外的元素,所以我要遍历每个内部ArrayList中相同数量的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM