java 初始化原语数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10775185/
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
Initialize array of primitives
提问by Muz
I am wondering,
我想知道,
What's exactly the difference between these two ways of initializing an array of primitives:
这两种初始化基元数组的方法之间到底有什么区别:
int[] arr1 = new int[]{3,2,5,4,1};
int[] arr2 = {3,2,5,4,1};
and which one is preferred ?
哪一个是首选?
采纳答案by T.J. Crowder
There is none, they produce exactly the same bytecode. I think it may be that the second form wasn't supported in older versions of Java, but that would have been a while back.
没有,它们产生完全相同的字节码。我认为可能是旧版本的 Java 不支持第二种形式,但那是很久以前的事了。
That being the case, it becomes a matter of style, which is a matter of personal preference. Since you specifically asked, Iprefer the second, but again, it's a matter of personal taste.
既然如此,那就成了风格问题,这是个人喜好的问题。既然你特别问了,我更喜欢第二个,但同样,这是个人品味的问题。
回答by Paul Bellora
As others have mentioned, they are equivalent and the second option is less verbose. Unfortunately the compiler isn't always able to understand the second option:
正如其他人所提到的,它们是等效的,第二个选项不那么冗长。不幸的是,编译器并不总是能够理解第二个选项:
public int[] getNumbers() {
return {1, 2, 3}; //illegal start of expression
}
In this case you have to use the full syntax:
在这种情况下,您必须使用完整的语法:
public int[] getNumbers() {
return new int[]{1, 2, 3};
}
回答by whileone
There is no difference between the two statements. Personally speaking, the second one is preferred. Because you have all the elements specified in the braces. The compiler will help you to compute the size of the array.
这两种说法没有区别。个人而言,首选第二种。因为您拥有大括号中指定的所有元素。编译器将帮助您计算数组的大小。
So no need to add int[]
after the assignment operator.
所以不需要int[]
在赋值运算符之后添加。
回答by David Chen
In your case, these two styles end up same effect, both correct, with the second one more concise. But actually these two styles are different.
在您的情况下,这两种样式最终效果相同,均正确,第二种样式更简洁。但实际上这两种风格是不同的。
Remember arrays in java are fixed length data structures. Once you create an array, you have to specify the length.
请记住,java 中的数组是固定长度的数据结构。创建数组后,必须指定长度。
Without initialization, the first case is
没有初始化,第一种情况是
int[] arr1 = new int[5];
The second case it would be
第二种情况是
int[] arr2 = {0,0,0,0,0};
You see the difference? In this situation, the first style is preferred as you don't have to type all those default initial values manually.
你看出区别了吗?在这种情况下,首选第一种样式,因为您不必手动键入所有这些默认初始值。
To me, the only big difference between the two styles is when creating an array without explicit initialization.
对我来说,这两种风格之间唯一的大区别是在没有显式初始化的情况下创建数组。
回答by goat
useful in this situation
在这种情况下很有用
void foo(int[] array) {
}
calling with a literal
用文字调用
// works
foo(new int[]{5, 7})
//illegal
foo({5, 7})
回答by Alex Lockwood
In this case, the second one because it's prettier and less verbose :)
在这种情况下,第二个是因为它更漂亮,更简洁:)