0

I need to create a two dimensional array of type java.lang.String using the reflection method from JavaScript code which is running inside a java application (inside the rhino scripting engine). This array will be a return value (of a javaScript function) that is used from JavaCode after the function call.

function test() {
    var a = java.lang.reflect.Array.newInstance(?, ?);
    // fill the array
    return a;
}

I couldn't find the right parameters for the newInstance call to create a two diemnsional array of type String.

At the moment I'm working with a workaround, i.e. I create an (outer) array of type java.lang.Objectof size x and inside a x-length loop a create multiple arrays of type java.lang.String each of size y which are assigned to the ''outer' array elements.

Is there an easier way?

1 Answer 1

1

All you have to do is fill in the class and dimensions:

var a = java.lang.reflect.Array.newInstance(String.class, x, y);

Read more in the javadoc for newInstance(Class<?> componentType, int... dimensions).

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

1 Comment

Thanks, I just didn't read the enough in the refelction API doc. It works perfectly!

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.