You can do this with Object [], restricting its members to either Object [] or int [].
For example, here is an array that runs three levels deep in one part and two levels deep in another:
Object[] myarray = new Object[] { new Object[] { new int[] { 1, 2 }, new int[] { 3, 4 }}, new int[] { 5, 6 } };
Once you have created it, you can access the elements. In your case, you know the depth N in front, so you know at what depth the object [] is expected, and at what depth int [] is expected.
However, if you do not know the depth, you can use reflection to determine if the element is a different level of Object [] or an int [] sheet.
if ( myarray[0] instanceof Object[] ) { System.out.println("This should print true."); }
EDIT:
Here's a sketch [untested so far, sorry] of a method that accesses an array element of a known depth, given an array of indices. The m_root element can be Object [] or int []. (You can relax this to support scalars.)
public class Grid { private int m_depth; private Object m_root; ... public int get( int ... indices ) { assert( indices.length == m_depth ); Object level = m_root; for ( int i = 0; i + 1 < m_depth; ++i ) { level = ((Object[]) level)[ indices[i] ]; } int[] row = (int[]) level; return row[ indices[m_depth - 1] ]; } }
source share