【发布时间】:2013-05-14 01:52:14
【问题描述】:
我有一张地图
private HashMap<Character, Integer> map;
我想将它转换为数组,但是当我这样做时/我得到了这个:
Entry<Character, Integer> t = map.entrySet().toArray();
**Type mismatch: cannot convert from Object[] to Map.Entry<Character,Integer>**
Entry<Character, Integer>[] t = null;
map.entrySet().toArray(t);
**Exception in thread "main" java.lang.NullPointerException**
Entry<Character, Integer>[] t = new Entry<Character, Integer>[1];
map.entrySet().toArray(t);
**Cannot create a generic array of Map.Entry<Character,Integer>**
Entry<Character, Integer>[] t = null;
t = map.entrySet().toArray(t);
**Exception in thread "main" java.lang.NullPointerException**
那么如何将HashMap 转换为Array? 在其他科目中找到的答案均无效。
【问题讨论】:
-
stackoverflow.com/questions/529085/… 问题是不可能创建像
Entry<Character, Integer>[]这样的通用数组。请改用List<Entry<Character, Integer>>。
标签: java arrays collections map toarray