0

I have a piece of code that returns an Object array. Each slice of the array then contains data that was initially a two dimensional array of doubles. I can't change this piece of code as it's part a Matlab thing (see link below).

http://www.mathworks.com/help/mps/java/code-multiple-outputs-for-java-client.html

My question is how do I convert each slice of the Object[] back into a double[][] within Java?

Here's some sample code

Object[] test = getDailyLog(1,20050101,20060101);
Object a = test[1];
System.out.println(a);

From this I get the output

[[D@17b90c55
3
  • What is the return type getDailyLog? Commented Jul 31, 2014 at 1:23
  • It's unclear from your description what sort of objects are actually in the array. You need to change your code snippet above to do System.out.println(a.getClass().getName());, to show the class of the objects. Then one can figure out how to extract the values. Commented Jul 31, 2014 at 2:01
  • 1
    (But the [[ prefix above suggests you have a two-dimensional array, and maybe the D means double (been a long time since I've looked at the default toString for an array).) Commented Jul 31, 2014 at 2:03

1 Answer 1

1

From your description, it could be inferred that 'test' is an array of "double[][]". Therefore, you could use the coersion cast to fulfil your requirement, example is listed as below:

        Object[] test = getDailyLog(1,20050101,20060101);

        double[][] d2d;
        for(Object obj : test) {
            d2d = (double[][]) obj;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Of course, that would extract all the elements into one array, writing one on top of the next. You really should extract into a 3-dimensional array.
More correctly, it would refer to each element using the same reference.
I changed it so that each Object gets it's own double array variable. Thanks!

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.