0

I have a map and trying to convert that to 2 dimension array. For this I am converting the values of Map to list and then trying to convert to 2 dimensional array.

My code is

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

when i do this I am getting class cast exception com.pojo.layout.LayoutDetails cannot be cast to java.util.ArrayList Is there any way to solve this. Is the conversion of List to 2-dimensional array right..? Here the layoutMap is Map<Integer, List>

Srikanth Sridhar
  • 2,317
  • 7
  • 30
  • 50

1 Answers1

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++;
}
Bogatyr
  • 36
  • 1