3

Why

int arr[][]=new int[5][];

declaration is perfectly fine but

int arr[][]=new int[][5]    

generates compile time error ?
Please do help me. I am not able to understand why is that so?

1 Answer 1

10

int arr[][] (which is more typically written as int[][] arr) is an array, each element of which is in turn a reference to an array.

new int[][5] would mean "create an array of unknown length, each element of which is a reference to an array, each of length 5". Obviously, that doesn't make sense.

On the other hand, new int[5][] means "create a length-5 array, each element of which is a null reference to an array".

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

Comments

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.