Java array assignment (multiple values)

后端 未结 10 1531
醉梦人生
醉梦人生 2020-12-24 06:45

I have a Java array defined already e.g.

float[] values = new float[3];

I would like to do something like this further on in the code:

10条回答
  •  情歌与酒
    2020-12-24 07:12

    Yes:

    float[] values = {0.1f, 0.2f, 0.3f};
    

    This syntax is only permissible in an initializer. You cannot use it in an assignment, where the following is the best you can do:

    values = new float[3];
    

    or

    values = new float[] {0.1f, 0.2f, 0.3f};
    

    Trying to find a reference in the language spec for this, but it's as unreadable as ever. Anyone else find one?

提交回复
热议问题