Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up coding the solution instead of attempting to find/use array manipulation methods from the Java API (the only one below is <code>java.util.Arrays.copyOf(Object[], int)</code>. </p> <p>So, below are the methods I wrote. I did not test them extensively (I wouldn't be surprised if there are inefficiencies there, I just tried the first thing that appeared to work), so I expect that the following solution can be adapted to suit others' needs:</p> <pre><code>/** * Creates an instance of Object multi-dimensional arrays, with dimensions specified by the argument. * * @example Create an array Object[4][20][30]: * &lt;pre&gt; * Object[] array = newArray(new int[]{4,20,30}); * &lt;/pre&gt; * * @param sizes The list of dimension lengths. * @return */ public static Object[] newArray(int[] sizes) { Object[] result = null; if (sizes != null) { for(int n = sizes.length - 1; n &gt;= 0; n--) { if (result == null) { result = new Object[sizes[n]]; } else { Object[] oa = new Object[sizes[n]]; for (int i = 0; i &lt; sizes[n]; i++) { oa[i] = java.util.Arrays.copyOf(result, result.length); } result = oa; } } if (result == null) { result = new Object[1]; } } return result; } /** * Get the value of a multi-dimensional array element given by a coordinate list. * * @example Read the value at [2][14][27]: * &lt;pre&gt; * Object[] array; // e.g. a int[4][20][30] created with newArray(int[]) * int[] coord = new int[]{2,14,27}; * Object value = getValue(array, coord); * &lt;/pre&gt; * * @param array The coordinates of the array element. * @param coordinates * @return */ public static Object getValue(Object[] array, int[] coordinates) { Object result = null; if (array == null || coordinates == null || 0 &gt; coordinates[0]||coordinates[0] &gt; array.length) { result = null; } else { int x = coordinates[0]; if (array[x] instanceof Object[]) { int[] c = new int[coordinates.length-1]; for(int i = 0; i &lt; c.length; i++) { c[i] = coordinates[i + 1]; } result = getValue((Object[]) array[x], c); } else { result = array[x]; } } return result; } /** * Set the value of a multi-dimensional array element given a list designating the element's coordinates. * * @example Write a value to [1][0][7]: * &lt;pre&gt; * Object value; // e.g. a float * Object[] array; // e.g. a int[4][20][30] created with newArray(int[]) * int[] coord = new int[]{1,0,7,}; * setValue(array, coord, value); * &lt;/pre&gt; * * @param array * @param coordinates * @param value * @return */ public static void setValue(Object[] array, int[] coordinates, Object value) { if (array == null || coordinates == null || array.length == 0 || coordinates.length == 0 || array.length &lt; coordinates[0]||coordinates[0] &lt; 0 ) { return; } else { int x = coordinates[0]; if (array[x] != null &amp;&amp; array[x].getClass().isArray()) { // recurse int[] c = new int[coordinates.length - 1]; for (int i = 0; i &lt; c.length; i++) { c[i] = coordinates[i + 1]; } setValue((Object[]) array[x], c, value); } else { array[x] = value; } } return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload