下面的java语句是什么意思
我正在审查 java 并偶然发现类似以下代码块的内容
public Foo example()
Foo bar = new Foo(...);
...
return new Foo[]{bar};
Foo[]{bar} 在这种情况下意味着什么?它是否返回由 bar 填充的 Foo 对象数组?似乎是一件非常微不足道的事情,但我不知道如何搜索它。
I'm reviewing java and stumbled upon something like the following block of code
public Foo example()
Foo bar = new Foo(...);
...
return new Foo[]{bar};
what does Foo[]{bar} mean in this context? Is it returning an array of Foo objects populated by bar? Seems to be something really trivial but I'm not sure what how to search for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,它正在创建一个包含单个元素的数组,最初设置为
bar
的值。不过,您可以将此语法用于更多元素,例如,这是一个 ArrayCreationExpression,如 Java 语言规范第 15.10 节,使用 中指定的ArrayInitializer href="http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.6" rel="nofollow">第 10.6 节。
Yes, it's creating an array with a single element, initially set to the value of
bar
. You can use this syntax for more elements though, e.g.This is an ArrayCreationExpression, as specified in section 15.10 of the Java Language Specification, using an ArrayInitializer as specified in section 10.6.
是的。它是一个只有一个元素的数组。该元素是
bar
。Yes. It's an array of one element. That element being
bar
.是的,没错。这将构造一个
Foo
的单元素数组,该数组仅由元素bar
组成。Yes, that's right. This constructs a single-element array of
Foo
that consists only of the elementbar
.等效的代码是
The equivalent code would be
它返回一个数组,内容为 bar。
It is returning an array with the contents being bar.