我的堆栈跟踪如下所示:
java.lang.IllegalArgumentException: Indexed or mapped properties are not supported on objects of type Map: get(1)
at org.apache.commons.beanutils.PropertyUtilsBean.getPropertyOfMapBean(PropertyUtilsBean.java:813)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:764)
at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382)
当我尝试访问地图的一部分时,我在这里得到它:
//Property Names
for ( int i=1; i < planMonthList.size(); i++ )
{
planTab.add("planPosition....get" + "(" + i + ")");
}
答案 0 :(得分:3)
您不能直接迭代地图。您必须遍历keySet
,entrySet
或values
集合
示例:强>
Map <String,Integer> m = new HashMap<String,Integer>();
m.put("A", 1);
m.put("B", 2);
m.put("C", 3);
// Iterate over keys
for (String key : m.keySet()) {
System.out.println("Key=["+key+"], value=["+m.get(key)+"]");
}
// Iterate over values
for (Integer value : m.values()) {
System.out.println("Value=["+value+"]");
}
// Iterate over entrySet
for (Map.Entry<String,Integer> entry : m.entrySet()) {
System.out.println("Key=["+entry.getKey()+"], value=["+entry.getValue()+"]");
}