0

我有一张地图并试图将其转换为二维数组。为此,我将 Map 的值转换为列表,然后尝试转换为二维数组。

我的代码是

try {
List list = new ArrayList(layoutMap.values());
int listSize = list.size();
ArrayList[][] layoutList = new ArrayList[listSize][];
for(int i = 0; i < listSize; i++){
    List sublist = (ArrayList) list.get(i);
    int subListSize = sublist.size();
    layoutList[i] = new ArrayList[subListSize];
    for (int j = 0; j < subListSize; j++) {
    layoutList[i][j] =  (ArrayList) sublist.get(j);
    }
} 
} catch (Exception e) {
log.error("@layoutMapDetails () :", e);
}

当我这样做时,我得到类转换异常com.pojo.layout.LayoutDetails cannot be cast to java.util.ArrayList有没有办法解决这个问题。List 到二维数组的转换是否正确..?这里的layoutMap是Map<Integer, List>

4

1 回答 1

0
final Object[][] result = new Object[map.size()][2];

final Iterator<?> iter = map.entrySet().iterator();

int ii = 0;
while(iter.hasNext()){
    final Map.Entry<?, ?> mapping = (Map.Entry<?, ?>) iter.next();

    result[ii][0] = mapping.getKey();
    result[ii][1] = mapping.getValue();

    ii++;
}
于 2012-06-04T12:16:45.390 回答