0
int arr[][] = new int[3][4];
int row[] = {1,2,3,4};
arr[0] = {1,2,3,4}; // why this doesn't work?
arr[0] = row; //when this line works

In above code, why we can't initialize row of array with {1,2,3,4} directly (on line3).

SrdjaNo1
  • 159
  • 1
  • 1
  • 10
  • https://stackoverflow.com/questions/1938101/how-to-initialize-an-array-in-java – Reimeus Jan 26 '18 at 11:42
  • https://stackoverflow.com/questions/5387643/array-initialization-syntax-when-not-in-a-declaration – Tom Jan 26 '18 at 11:43
  • That is simply how the language is specified. After the variable is declared, you have to say `arr[0] = new int{...}`. – Andy Turner Jan 26 '18 at 11:43
  • `{1,2,3,4}` Is called as array literal and you cannot use it without denoting it's type left of right side. That is how they got designed. – Suresh Atta Jan 26 '18 at 11:44
  • @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ no, it's called an [Array initializer](https://docs.oracle.com/javase/specs/jls/se9/html/jls-10.html#jls-10.6). – Andy Turner Jan 26 '18 at 11:45
  • https://stackoverflow.com/questions/10520617/why-can-array-constants-only-be-used-in-initializers – Noixes Jan 26 '18 at 11:45
  • In another words, for each row you have to use `new int[4]`, or explicitly initialize that row (like you did in the last statement). Good luck, Срђане – zlakad Jan 26 '18 at 11:48

0 Answers0