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

后端 未结 3 1052
暖寄归人
暖寄归人 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:43

    It's only acceptable during a declaration. You can, however, use new double[] {0, 1, 2}.

    JLS §10.6:

    An array initializer may be specified in a declaration, or as part of an array creation expression.

    An array creation expression is the new double[] { } syntax.

提交回复
热议问题