Java数组赋值(多个值) [英] Java array assignment (multiple values)
本文介绍了Java数组赋值(多个值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经定义了一个Java数组,例如。
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:
values = {0.1f, 0.2f, 0.3f};
但是这给了我一个编译错误。有没有更好的方法一次定义多个值,而不是这样做?:
But that gives me a compile error. Is there a nicer way to define multiple values at once, rather than doing this?:
values[0] = 0.1f;
values[1] = 0.2f;
values[2] = 0.3f;
谢谢!
推荐答案
是:
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];
或
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?
这篇关于Java数组赋值(多个值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文