Java 数组赋值(多个值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2576038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java array assignment (multiple values)
提问by Danny King
I have a Java array defined already e.g.
我已经定义了一个 Java 数组,例如
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;
Thanks!
谢谢!
采纳答案by skaffman
Yes:
是的:
float[] values = {0.1f, 0.2f, 0.3f};
This syntax is onlypermissible 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?
试图在语言规范中为此找到参考,但它和以往一样不可读。还有人找吗?
回答by bmargulies
values = new float[] { 0.1f, 0.2f, 0.3f };
回答by Julian Lettner
On declaration you can do the following.
在声明时,您可以执行以下操作。
float[] values = {0.1f, 0.2f, 0.3f};
When the field is already defined, try this.
当字段已经定义时,试试这个。
values = new float[] {0.1f, 0.2f, 0.3f};
Be aware that also the second version creates a new array.
If valueswas the only reference to an already existing field, it becomes eligible for garbage collection.
请注意,第二个版本也会创建一个新数组。如果values是对现有字段的唯一引用,则它有资格进行垃圾收集。
回答by fastcodejava
If you know the values at compile time you can do :
如果您知道编译时的值,您可以执行以下操作:
float[] values = {0.1f, 0.2f, 0.3f};
There is no way to do that if values are variables in runtime.
如果值是运行时的变量,则无法做到这一点。
回答by Stephen C
Java does not provide a construct that will assignof multiple values to an existing array's elements. The initializer syntaxes can ONLY be used when creation a new array object. This can be at the point of declaration, or later on. But either way, the initializer is initializing a new array object, not updating an existing one.
Java 不提供将多个值分配给现有数组元素的构造。初始化器语法只能在创建新数组对象时使用。这可以在声明时进行,也可以在稍后进行。但无论哪种方式,初始化器都在初始化一个新的数组对象,而不是更新现有的数组对象。
回答by Bart van Heukelom
This should work, but is slower and feels wrong: System.arraycopy(new float[]{...}, 0, values, 0, 3);
这应该可以工作,但速度较慢并且感觉不对: System.arraycopy(new float[]{...}, 0, values, 0, 3);
回答by bou22
You may use a local variable, like:
您可以使用局部变量,例如:
float[] values = new float[3];
float[] v = {0.1f, 0.2f, 0.3f};
float[] values = v;
回答by Genc Gashi
public class arrayFloats {
public static void main (String [] args){
float [] Values = new float[3];
float Incre = 0.1f;
int Count = 0;
for (Count = 0;Count<3 ;Count++ ) {
Values [Count] = Incre + 0.0f;
Incre += 0.1f;
System.out.println("Values [" + Count + "] : " + Values [Count]);
}
}
}
//OUTPUT:
//Values [0] : 0.1
//Values [1] : 0.2
//Values [2] : 0.3
This isn't the all and be all of assigning values to a specific array. Since I've seen the sample was 0.1 - 0.3 you could do it this way. This method is very useful if you're designing charts and graphs. You can have the x-value incremented by 0.1 until nth time.
这不是将值分配给特定数组的全部内容。因为我看到样本是 0.1 - 0.3,所以你可以这样做。如果您正在设计图表和图形,此方法非常有用。您可以将 x 值增加 0.1,直到第 n 次。
Or you want to design some grid of some sort.
或者您想设计某种网格。
回答by Utkarsh Prajapati
int a[] = { 2, 6, 8, 5, 4, 3 };
int b[] = { 2, 3, 4, 7 };
if you take float number then you take float and it's your choice
如果您采用浮点数,那么您采用浮点数,这是您的选择
this is very good way to show array elements.
这是显示数组元素的好方法。
回答by Hussein Muhammed
for example i tried all above for characters it fails but that worked for me >> reserved a pointer then assign values
例如,我尝试了以上所有字符,但它失败了,但这对我有用>> 保留一个指针然后分配值
char A[];
A = new char[]{'a', 'b', 'a', 'c', 'd', 'd', 'e', 'f', 'q', 'r'};

