Retrieve all values from HashMap keys in an ArrayList Java

前端 未结 9 1396
野性不改
野性不改 2021-01-31 14:21

Good day, this is kind of confusing me now(brain freeze!) and seem to be missing something. Have an ArrayList which i populate with a HashMap. now i put in my HashMap and arrayl

9条回答
  •  醉酒成梦
    2021-01-31 14:54

    Put i++ somewhere at the end of your loop.

    In the above code, the 0 position of the array is overwritten because i is not incremented in each loop.

    FYI: the below is doing a redundant search:

    if(keys[i].equals(DATE)){                 
       date_value[i] = map.get(keys[i]);              
    } else if(keys[i].equals(VALUE)){              
       value_values[i] = map.get(keys[i]);             
    } 
    

    replace with

    if(keys[i].equals(DATE)){                 
       date_value[i] = mapping.getValue();
    } else if(keys[i].equals(VALUE)){              
       value_values[i] = mapping.getValue()
    } 
    

    Another issue is that you are using i for date_value and value_values. This is not valid unless you intend to have null values in your array.

提交回复
热议问题