Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <pre><code>/** * Clones the provided array * * @param src * @return a new clone of the provided array */ public static int[][] cloneArray(int[][] src) { int length = src.length; int[][] target = new int[length][src[0].length]; for (int i = 0; i &lt; length; i++) { System.arraycopy(src[i], 0, target[i], 0, src[i].length); } return target; } </code></pre> </blockquote> <p>Is it possible to modify this code to support <strong>n-dimensional arrays of Objects</strong>?</p> <p>You would need to support arbitrary lengths of arrays and check if the src and destination have the same dimensions, <s>and you would also need to copy each element of each array <em>recursively</em>, in case the Object was also an array.</s></p> <p>It's been a while since I posted this, but I found a <strong><a href="https://stackoverflow.com/questions/4770926/java-n-dimensional-arrays">nice example</a></strong> of one way to create an n-dimensional array class. The class takes zero or more integers in the constructor, specifying the respective size of each dimension. The class uses an underlying <em>flat</em> array <code>Object[]</code> and calculates the index of each element using the dimensions and an array of multipliers. (This is how arrays are done in the C programming language.) </p> <p>Copying an instance of <code>NDimensionalArray</code> would be as easy as copying any other 2D array, though you need to assert that each <code>NDimensionalArray</code> object has equal dimensions. This is probably the easiest way to do it, since there is no recursion, and this makes representation and access much simpler.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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