定义数组时使用大括号
关于以下代码:
int[] to = new int[] { text };
我理解它尝试定义一个整数数组,但是花括号在数组定义中起什么作用?
Regards to the following code:
int[] to = new int[] { text };
I understand it tries to define an array of integer, but What does the curly braces do in array definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这只是创建具有初始元素的数组的快捷方式代码,以下内容(相同):
可以替换为
希望这会有所帮助。
This is just a shortcut code to create an array with initial elements, the followings (which are equal):
can be substituted with
Hope this helps.
大括号包含填充数组的值。
The curly braces contain values to populate the array.
此语法允许您定义数组的内容,通常称为数组文字。
在这种情况下,这实际上可以简化为:
仅当不属于赋值的一部分时才需要在其之前添加
new int[]
,例如:This syntax allows you to define the contents of an array and is often referred to as an array literal.
In this context this can actually be simplified to:
Adding
new int[]
before it is only required when not part of an assignment, something like:大括号向编译器表示数组的值
Curly braces said to the compiler the values of the array
正如 SLaks 所说,花括号是 Java 表示集合的一种方式。您可以使用此方法定义数组的内容,但您定义的每个元素必须与数组具有相同的类型。
Like SLaks said, curly braces is a way Java denotes a set. You can define the contents of the array using this method, but each element you define has to be the same type as the array.