繁体   English   中英

如何初始化地图 <Integer, Map<Integer, Float> &gt;?

[英]How to Initialize Map<Integer, Map<Integer, Float>>?

我想对Map<Integer, Map<Integer,Float>> 这是我初始化的方式:

Map<Integer, Map<Integer,Float>> map = new TreeMap<>();
for(int i=0; i<100; i++)
    map.put(i, new TreeMap<>());

但是我总是为entrySet获得null 当我尝试添加元素时

map.get(i).put(j.getKey(), d);

其中ijMap.Entry<Integer, Point3f> ,它什么也不做。 Point3f是库vecmath的对象)

编辑:我将其更改为HashMap但现在我得到NullPointerException

老实说,我不确定为什么entrySet()似乎为您返回null ,但是地图初始化似乎对我有用,对我来说还不错。

问题出在map.get(i).put(j.getKey(), d); ,对于您所说的ij类型。 您将map声明为Map<Integer, Map<Integer, Float>> ,这意味着map具有Integer键和Map<Integer, Float>值。 因此,当您使用键iMap.Entry<Integer, Point3f>调用get(i)时, map找不到与该特定键相对应的条目,因此它返回null 然后,当您尝试调用您认为已获得的地图上的put时,会收到NullPointerException

Map<Integer, Map<Integer,Float>> map = new TreeMap<Integer, Map<Integer,Float>>();

Map<Integer,Float> f =  map.get(5);
f.put(4,5.6f);

暂无
暂无

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

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