JAVA - Map类型的对象不支持索引或映射属性:

时间:2012-11-14 21:49:16

标签: java apache-commons-beanutils

我的堆栈跟踪如下所示:

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 + ")");
}

1 个答案:

答案 0 :(得分:3)

您不能直接迭代地图。您必须遍历keySetentrySetvalues集合

示例:

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()+"]");
}