Java 的隐藏特性 - ArrayList / 实例初始化器说明
在Java的隐藏功能问题中,我对有关实例初始值设定项的答案。
我想知道如何修改这一行:
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};
以便使其与嵌套的 Arraylists 执行相同的工作:
ArrayList<ArrayList<Integer>> numbers = ...
这可能吗?
In the Hidden Features of Java question, I was interested in the answer about instance initializers.
I was wondering how to modify this line :
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};
in order to make it perform the same job with nested Arraylists:
ArrayList<ArrayList<Integer>> numbers = ...
is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我真的不推荐这种方法,因为它无缘无故地创建了一个(匿名)类。
使用:
或
对于 2 个级别,您可以使用:
通过静态导入,您甚至可以将其减少为这样,如果您确实想要:
I don't really recommend this approach, as it creates an (anonymous) class for no good reason.
Use either:
or
For 2 levels, you can use:
With a static import you could even reduce it to this, if you really want to:
由于 Arrays.asList 采用可变参数,双括号初始化对于列表来说不再那么有趣,您可以这样做:
或者与双括号初始化结合使用,如下所示:
但它是否适合一行取决于您项目的代码格式指南。这是非常冗长的,以至于替代方案开始看起来不错。就像将数字放入字符串并解析它一样。或者切换到 Scala、Groovy 或 Clojure,它们都支持文字列表语法。
Double-brace initialization got a lot less interesting for lists now that Arrays.asList takes varargs, you can do this instead:
Or combine with double-brace initialization like this:
but whether it fits on one line depends on your project's code-formatting guidelines. This is so horribly verbose that alternatives start to look good. Like putting the numbers into a string and parsing it. Or switching to Scala, Groovy, or Clojure, all of whom support literal list syntax.
当然,您可以对内部 ArrayLists 执行相同的操作:,尽管我认为它变得非常不可读...
尽管不会比传统方式更长或更差(取决于您的大括号样式!)
Arrays.asList(1, 2 )其他答案中给出的方法似乎是最好的。
And of course you can do the same for the inner ArrayLists:, though I think it becomes pretty unreadable...
though not wildly longer or worse than the conventional way (depending on your brace style!)
The Arrays.asList(1, 2) approach given in other answers seems nicest of all.
我不会使用这个习惯用法来“简化”ArrayList 声明。它看起来很酷,但它不必要地创建了 ArrayList 的匿名子类,而且对于我的口味来说,它看起来太“神奇”了……您希望代码能够被下一个开发人员读取和理解。
您应该考虑使用 Google Guava 等库。
这是使用 Guava 的 Lists.newArrayList(E... elements) 实用方法:
或者,更好的是,仅使用左侧的
List
接口(您应该避免在变量声明中使用具体类):如果您知道这些列表是不可变的,那么最好使用 ImmutableList:
I would not use this idiom to "simplify"
ArrayList
declarations. It looks cool, but it needlessly creates an anonymous subclass ofArrayList
, and it looks too "magical" for my tastes... You want code that is both readable and understandable by the next developer.You should instead consider using a library such as Google Guava.
Here is an example using Guava's Lists.newArrayList(E... elements) utility method:
Or, even better, using only the
List
interface on the left-hand side (you should avoid using concrete classes in your variable declarations):If you know these lists will be immutable, it's even better to use ImmutableList:
只需将
int
替换为ArrayList
对象即可Just replace
int
with object ofArrayList<integer>