user36800
user36800

Reputation: 2259

Initialize Java Object array element with 2D double array literal

For a certain API [1], arrays of data have to be passed as elements of a Java Object array (one such element per array). The following works fine, and the 2D double array AInData is accepted by the API as the 1st argument (and there is only 1 argument):

double[][] AInData = {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}};
Object[] arObj = { AInData };

Since floating point literals default to double, I thought I could do without the intermediate variable AInData:

Object[] arObj = { {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}} };

This caused the following errors:

illegal initializer for Object
Object[] arObj = { {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}} };
                   ^
illegal initializer for <none>
Object[] arObj = { {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}} };
                    ^
illegal initializer for <none>
Object[] arObj = { {{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}} };
                                     ^

Why can I not do away with AInData?

If literals truly default to double, then there is no casting when AInData is initialized.

NOTES:

[1] Interface to Matlab

Upvotes: 0

Views: 74

Answers (1)

Andrei Kovrov
Andrei Kovrov

Reputation: 2440

Actually, you can

Object [] arObj = new double [][][] {{{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}} };

Upvotes: 1

Related Questions