Is it possible to dynamically build a multi-dimensional array in Java?

20,545

Solution 1

It is actually possible to do in java. (I'm a bit surprised I must say.)

Disclaimer; I never ever want to see this code anywhere else than as an answer to this question. I strongly encourage you to use Lists.

import java.lang.reflect.Array;
import java.util.*;

public class Test {

    public static int[] tail(int[] arr) {
        return Arrays.copyOfRange(arr, 1, arr.length);
    }

    public static void setValue(Object array, String value, int... indecies) {
        if (indecies.length == 1)
            ((String[]) array)[indecies[0]] = value;
        else
            setValue(Array.get(array, indecies[0]), value, tail(indecies));
    }

    public static void fillWithSomeValues(Object array, String v, int... sizes) {
        for (int i = 0; i < sizes[0]; i++)
            if (sizes.length == 1)
                ((String[]) array)[i] = v + i;
            else
                fillWithSomeValues(Array.get(array, i), v + i, tail(sizes));
    }

    public static void main(String[] args) {

        // Randomly choose number of dimensions (1, 2 or 3) at runtime.
        Random r = new Random();
        int dims = 1 + r.nextInt(3);

        // Randomly choose array lengths (1, 2 or 3) at runtime.
        int[] sizes = new int[dims];
        for (int i = 0; i < sizes.length; i++)
            sizes[i] = 1 + r.nextInt(3);

        // Create array
        System.out.println("Creating array with dimensions / sizes: " +
                Arrays.toString(sizes).replaceAll(", ", "]["));
        Object multiDimArray = Array.newInstance(String.class, sizes);

        // Fill with some 
        fillWithSomeValues(multiDimArray, "pos ", sizes);

        System.out.println(Arrays.deepToString((Object[]) multiDimArray));


    }
}

Example Output:

Creating array with dimensions / sizes: [2][3][2]
[[[pos 000, pos 001], [pos 010, pos 011], [pos 020, pos 021]],
 [[pos 100, pos 101], [pos 110, pos 111], [pos 120, pos 121]]]

Solution 2

Arrays are type-safe in java - that applies to simple arrays and "multi-dimensional" arrays - i.e. arrays of arrays.

If the depth of nesting is variable at runtime, then the best you can do is to use an array that corresponds to the known minimum nesting depth (presumably 1.) The elements in this array with then either be simple elements, or if further nesting is required, another array. An Object[] array will allow you to do this, since nested arrays themselves are also considered Objects, and so fit within the type system.

If the nesting is completely regular, then you can preempt this regularity and create an appropriate multimensional array, using Array.newInstance(String.class, dimension1, dimension2, ...), If nesting is irregular, you will be better off using nested lists, which allow for a "jagged" structure and dynamic sizing. You can have a jagged structure, at the expence of generics. Generics cannot be used if the structure is jagged since some elements may be simple items while other elements may be further nested lists.

Solution 3

So you can pass multiple dimensions to Array.newInstance, but that forces a fixed length for each dimension. If that's OK, you can use this:

// We already know from scanning the input that we need a 2 x 4 array.
// Obviously this array would be created some other way. Probably through
// a List.toArray operation.
final int[] dimensions = new int[2];
dimensions[0] = 2;
dimensions[1] = 4;

// Create the array, giving the dimensions as the second input.
Object array = Array.newInstance(String.class, dimensions);

// At this point, array is a String[2][4].
// It looks like this, when the first dimension is output:
// [[Ljava.lang.String;@3e25a5, [Ljava.lang.String;@19821f]
//
// The second dimensions look like this:
// [null, null, null, null]

The other option would be to build them up from the bottom, using getClass on the previous level of array as the input for the next level. The following code runs and produces a jagged array as defined by the nodes:

import java.lang.reflect.Array;

public class DynamicArrayTest
{
    private static class Node
    {
        public java.util.List<Node> children = new java.util.LinkedList<Node>();
        public int length = 0;
    }

    public static void main(String[] args)
    {
        Node node1 = new Node();
        node1.length = 1;

        Node node2 = new Node();
        node2.length = 2;

        Node node3 = new Node();
        node3.length = 3;

        Node node4 = new Node();
        node4.children.add(node1);
        node4.children.add(node2);

        Node node5 = new Node();
        node5.children.add(node3);

        Node node6 = new Node();
        node6.children.add(node4);
        node6.children.add(node5);

        Object array = createArray(String.class, node6);
        outputArray(array); System.out.println();
    }

    private static Object createArray(Class<?> type, Node root)
    {
        if (root.length != 0)
        {
            return Array.newInstance(type, root.length);
        }
        else
        {
            java.util.List<Object> children = new java.util.ArrayList<Object>(root.children.size());
            for(Node child : root.children)
            {
                children.add(createArray(type, child));
            }

            Object array = Array.newInstance(children.get(0).getClass(), children.size());
            for(int i = 0; i < Array.getLength(array); ++i)
            {
                Array.set(array, i, children.get(i));
            }

            return array;
        }
    }

    private static void outputArray(Object array)
    {
        System.out.print("[ ");
        for(int i = 0; i < Array.getLength(array); ++i)
        {
            Object element = Array.get(array, i);
            if (element != null && element.getClass().isArray())
                outputArray(element);
            else
                System.out.print(element);

            System.out.print(", ");
        }
        System.out.print("]");
    }
}

Solution 4

As a further note, what if we were to try something like this:

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);
...

No, you can't set an String[] value like that. You run into

Exception in thread "main" java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at Test.main(Test.java:12)

Solution 5

Ok, if you are unsure of the dimensions of the array, then the following method won't work. However, if you do know the dimensions, do not use reflection. Do the following:

You can dynamically build 2d arrays much easier than that.

int x = //some value
int y = //some other value

String[][] arr = new String[x][y];

This will 'dynamically' create an x by y 2d array.

Share:
20,545
Jordan
Author by

Jordan

Updated on October 03, 2020

Comments

  • Jordan
    Jordan over 3 years

    Suppose we have the Java code:

    Object arr = Array.newInstance(Array.class, 5);
    

    Would that run? As a further note, what if we were to try something like this:

    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);
    

    Would arr1 then be a 2D array equivalent to:

    String[2][4] arr1;
    

    How about this: what if we don't know the dimensions of this array until runtime?

    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

    [value1, value2, ...]
    

    or

    [ [value11, value12, ...] [value21, value22, ...] ...]
    

    And so on

    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...

    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);
    

    And so on. It just has to be a giant nested array of Objects

    • Srinivas Reddy Thatiparthy
      Srinivas Reddy Thatiparthy about 14 years
      that's what Collections are for...look at ArrayList<String>
    • Jordan
      Jordan about 14 years
      Okay, valid point. What if we need to have this in a native array form and not an ArrayList. If it is a multi-dimension ArrayList, will toArray handle the nested Lists?
    • aioobe
      aioobe about 14 years
      No, it will give you an array of ArrayLists.
    • OscarRyz
      OscarRyz about 14 years
      to have a primitive array ( String[][] ) you use : List.toArray method, see my answer.
    • Lightfire228
      Lightfire228 almost 7 years
      Just figured out from this that you can create an Object[] array, which can hold arrays, since arrays are objects. This means you can arbitrarily nest arrays without explicit array Dimension typing
  • Jordan
    Jordan about 14 years
    Check my edit, this might not be possible for the amount of unknown
  • Peter Recore
    Peter Recore about 14 years
    I think the hard part is making the dimension dynamic, not the number of items in each dimension.
  • Chris Dennett
    Chris Dennett about 14 years
    You still have to initalise the sub-arrays AFAIK. It's basically arrays of arrays :)
  • Jordan
    Jordan about 14 years
    @Peter, spot on. We aren't supposed to assume either the number of dimensions or the size of any dimension
  • Jordan
    Jordan about 14 years
    Yeah, I didn't bother testing that part. I was just hoping to get the point across :P
  • Jordan
    Jordan about 14 years
    This works perfectly, and given a regular array there's even a possibility of converting these Objects representing multi-dimensional String arrays into any mess of String[][][][][]...[]s. Thanks!
  • aioobe
    aioobe about 14 years
    If the depth of nesting is variable at runtime, then the best you can do is to use an array that corresponds to the known minimum nesting depth (presumably 1.) -- No, you can actually solve it. See my answer.
  • aioobe
    aioobe about 14 years
    // At this point, array is a String[2][4]. -- Sure, it will be of type String[][] but it will equal { null, null }.
  • aioobe
    aioobe about 14 years
    Right, the number of dimensions is determined runtime.
  • Jordan
    Jordan about 14 years
    So, while the last answer I had tagged was spiritually correct, I find it very hard to argue with a real, tested piece of code. Bravo
  • aioobe
    aioobe about 14 years
    I had no idea about Array.newInstance. If you hadn't mentioned that method in the question, I had probably said it was impossible... So it was a joint effort ;-)
  • Jordan
    Jordan about 14 years
    Neither did I, I found it while looking through the Array API praying for a built-in :P Also, nerd-snipe: now do this in C :D
  • aioobe
    aioobe about 14 years
    Well I'm no C expert, but I believe it is trivial, since arr[3][5] is equivalent to arr[15] in C :-)
  • jdmichal
    jdmichal about 14 years
    That is verifiably false. I just ran the code (fixing my mistake with doing int[0] instead of dimension[0]) outputting the values, and I get [[Ljava.lang.String;@3e25a5, [Ljava.lang.String;@19821f] as output, showing an array of two arrays of strings. Each of the two lower arrays was initialized with [null, null, null, null], as expected.
  • Ar5hv1r
    Ar5hv1r about 14 years
    Bravo, what a hack job! Let's pray no one ever has to use this. +1
  • aioobe
    aioobe about 14 years
    Oh, you are right indeed! Thanks for pointing that out. I'll update my post.
  • jdmichal
    jdmichal about 14 years
    Also, completely agree that this is vastly easier in C/C++. Just create an array to hold all the leaf elements, then pretend it's a multi-dimensional array. If it's a jagged array, you will have to do something similar to what aioobe does here, using pointers and new over Array.newInstance.
  • mdma
    mdma about 14 years
    @aioobe - your answer is equivalent to what I am saying. The type safety is not being used and instead a basic object array is assumed at each case, and that you are using a fixed nesting (no jagged arrays) so that the last level can be safely assumed a string array.
  • aioobe
    aioobe about 14 years
    ok, not sure what you're meaning, but no object-arrays are involved. It's arrays of String, arrays of String[] and arrays of String[][] and so on, never an Object[].
  • OscarRyz
    OscarRyz about 14 years
    +1 While clever, there is no need ( at least in OP scenario ) to use reflection when java.util may do the work. See my answer. I UV for the effort anyway.
  • aioobe
    aioobe about 14 years
    But does this approach create String[][][] appropriately if the input is, say, [[[1]]]?
  • OscarRyz
    OscarRyz about 14 years
    Definitely. The principle is the same; create an ArrayList and put it inside another, the parsing would be different of course. Going further, if the number of inner arrays is unknow, it would be much better ( if not the only way ) to have an ArrayList as data structure, because you won't be able to define the type of your array ( there is no String[] or String[]? ) . To define the type of the result, you have to specify the number of inner arrays like in String[][][][] ( 4 )