2

I have a String which contains arrays. Example:

"[[1, 2], [4, 5], [7, 8]]"

Now, I want to make an actual Java array out of this. I have created a function to get the dimensions and a recursive function to get each and every element of the array. Thus, I have every 1-D array created, but I would like to know if there is a way in Java to create an array of the dimension that I found during the runtime? The dimension are returned as an array. Like, for the above example the dimension is:

[3, 2]

EDIT: Is there a way to create an actual array from this information? The dimension [3, 2] is just an example. I can have [3, 2, 4, 5] as well. Can an array be generated from this information during the runtime? I do not have this information during compile time.

There is some problem, I cannot comment on answers. So, I am editing here.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
Rishi
  • 1,987
  • 6
  • 32
  • 49
  • what do you do with newly created array? – vels4j Jul 10 '13 at 07:54
  • 1
    Are there always two dimensions? Or could it be "[[[1,2],[3,4]][[5,6],[7,8]][[9,10]] as well? – Angelo Fuchs Jul 10 '13 at 07:56
  • 2
    "create an array of the dimension that I found during the runtime" - do you mean create an array without knowing it's dimension at compile time? i.e. your input string might be a 3 or 4 dimensional array? – selig Jul 10 '13 at 07:56

2 Answers2

2

If you are doing numeric work then you should probably use a library. For example my open source library Vectorz provides proper support for multidimensional arrays / matrices (with doubles).

Then you can just do something like:

INDArray m=Arrayz.parse("[[1, 2], [4, 5], [7, 8]]");

The INDArray object encapsulates all the multidimensionality so you don't need to worry about writing big nested loops all the time, plus it provides a lot of other functions and capabilities that normal Java arrays don't have.

mikera
  • 105,238
  • 25
  • 256
  • 415
1

The main problem is that you can't reference an N dimensional array in code directly.

Fortunately java has a bit of a dirty hack that lets you have some way to work with N dimensional arrays:

Arrays are objects like every other non-primative in java.

Therefore you can have:

// Note it's Object o not Object[] o
Object o = new Object [dimensionLengh];

You can use a recursive method to build it up.

Object createMultyDimArray(int [] dimensionLengths) {
    return createMultyDimArray(dimensionLengths, 0);
}

Object createMultyDimArray(int [] dimensionLengths, int depth) {
    Object [] dimension = new Object[dimensionLengths[depth]];
    if (depth + 1 < dimensionLengths.length) {
        for (int i=0; i < dimensionLengths[depth]; i++) {
            dimension[i] = createMultyDimArray(dimensionLengths, depth+1);
        }
    }
    return dimension;
}

To use this you will need to cast from Object to Object [].

Java is type safe on arrays and mistakes can cause a ClassCastException. I would recommend you also read about how to create arrays using java reflection: http://docs.oracle.com/javase/tutorial/reflect/special/arrayInstance.html.

Philip Couling
  • 13,581
  • 5
  • 53
  • 85