Java: create a list of HashMaps

前端 未结 6 553
甜味超标
甜味超标 2021-01-02 02:36

I tried to create a list of maps. In the following code, I\'m expecting to get

[{start=1,text=ye}, {start=2,text=no}]

however, I only got

6条回答
  •  执笔经年
    2021-01-02 02:43

    have three adds to the list. the first add is to a new map instance; you never set any values. The second add you pass in a reference to nMap, which has 1, yes. The third add you pass the same reference. So the Map now has 3 references, the first to a map you never added any values to, the next 2 to the same map. which is why you get the same output.

提交回复
热议问题