Cast to multidimensional arrays

From: Jay (rt987_at_hotmail.com)
Date: 01/30/04


Date: 30 Jan 2004 08:13:04 -0800

Why does the following code produce a ClassCastException?

ArrayList list = new ArrayList();
list.add(new Object[]{"Test1", "..."});
list.add(new Object[]{"Test2", "..."});
list.add(new Object[]{"Test3", "..."});

Object ar[][] = (Object[][]) list.toArray();

The last line causes a ClassCastException... Yet, list.toArray() is
just an array of objects which are themselves arrays of objects. So
list.toArray is an Object[][], conceptually. Why does Java complain?