title: Is it possible to dynamically build a multi-dimensional array in Java?
body: <p>Suppose we have the Java code:</p>
  
  <pre><code>Object arr = Array.newInstance(Array.class, 5);
  </code></pre>
  
  <p>Would that run? As a further note, what if we were to try something like this:</p>
  
  <pre><code>Object arr1 = Array.newInstance(Array.class, 2);
  Object arr2 = Array.newInstance(String.class, 4);
  Object arr3 = Array.newInstance(String.class, 4);
  Array.set(arr1, 0, arr2);
  Array.set(arr1, 1, arr3);
  </code></pre>
  
  <p>Would arr1 then be a 2D array equivalent to:</p>
  
  <pre><code>String[2][4] arr1;
  </code></pre>
  
  <p>How about this: what if we don't know the dimensions of this array until runtime?</p>
  
  <p>Edit: if this helps (I'm sure it would...) we're trying to parse an array of unknown dimensions from a String of the form</p>
  
  <pre><code>[value1, value2, ...]
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[ [value11, value12, ...] [value21, value22, ...] ...]
  </code></pre>
  
  <p>And so on</p>
  
  <p>Edit2: In case someone as stupid as I am tries this junk, here's a version that at least compiles and runs. Whether or not the logic is sound is another question entirely...</p>
  
  <pre><code>Object arr1 = Array.newInstance(Object.class, x);
  Object arr11 = Array.newInstance(Object.class, y);
  Object arr12 = Array.newInstance(Object.class, y);
  ...
  Object arr1x = Array.newInstance(Object.class, y);
  Array.set(arr1, 0, arr11);
  Array.set(arr1, 1, arr12);
  ...
  Array.set(arr1, x-1, arr1x);
  </code></pre>
  
  <p>And so on. It just has to be a giant nested array of Objects</p>