In Java, when is the {a,b,c,…} array shorthand inappropriate, and why?

后端 未结 3 1053
暖寄归人
暖寄归人 2021-01-17 09:53

If you\'re defining a variable, it appears to be perfectly valid to declare/define a variable as follows:

    double[][] output = {{0,0},{1,0}};
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-17 10:37

    You can use braces notation only at the point of declaration, where compiler can infer the type of array from the declaration type.

    To use it anywhere else you need to use Array Creation Expression:

    return new double[] {0,1,2};
    

提交回复
热议问题