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.

3
  • what do you do with newly created array? Commented Jul 10, 2013 at 7:54
  • 1
    Are there always two dimensions? Or could it be "[[[1,2],[3,4]][[5,6],[7,8]][[9,10]] as well? Commented Jul 10, 2013 at 7: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? Commented Jul 10, 2013 at 7:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

nice link. You should have stated that it is your library, if it is. (It seems so in the link)
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.

2 Comments

I got a StackOverflow error in this line: dimension[i] = createMultyDimArray(dimensionLengths, depth); when calling this with arguments new int[]{2,3,4,5}
I think you forgot to increase depth (you may wanted to use i in the line)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.