Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 520
Jordan Avatar asked Jun 23 '10 18:06

Jordan


People also ask

How do you create a multidimensional array in Java dynamically?

String [][] stringArray = allocate(String. class,3,3); This will give you a two dimensional String array with 3 rows and 3 columns; Note that in Class<tType> c -> c cannot be primitive type like say, int or char or double . It must be non-primitive like, String or Double or Integer and so on.

Can array grow dynamically in Java?

An array cannot be resized dynamically in Java.

Is dynamic array possible?

A dynamic array is an array with a big improvement: automatic resizing. One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

Can arrays be multidimensional in Java?

No, Java does not support multi-dimensional arrays. Java supports arrays of arrays. In Java, a two-dimensional array is nothing but, an array of one-dimensional arrays.


1 Answers

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]]] 
like image 147
aioobe Avatar answered Oct 15 '22 04:10

aioobe