2

I have a constructor that expects a nested array of doubles like this:

public class Thing {

    public Thing (double[][] doubles) {
    }
}

I would like to overload the constructor with a default constructor that takes no arguments like this:

    public Thing () {
        this(/* TODO */);
    }

1 Answer 1

3

You have to nest a new one-dimensional double array into another two dimensional double array. That way, you can initialize the inner, one-dimensional one manually and the outer two dimensional one with it.

    public Thing () {
        this(new double[][]{new double[]{1}});
    }

You can also initialize more than one double:

        this(new double[][]{new double[]{1, 2, 3}});

or even:

        this(new double[][]{new double[]{1, 2, 3},
                            new double[]{4, 5, 6}});
Sign up to request clarification or add additional context in comments.

1 Comment

Or new double[0][0] or size you want in the brackets.

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.