用于实例化初始化集合的紧凑语法 [英] Compact syntax for instantiating an initializing collection

查看:81
本文介绍了用于实例化初始化集合的紧凑语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种紧凑的语法,用于实例化集合并向其中添加一些项目。我目前使用这种语法:

I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax:

Collection<String> collection = 
    new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" }));

我似乎记得有一种更紧凑的方法,它使用<$的匿名子类c $ c> ArrayList ,然后将项目添加到子类的构造函数中。但是,我似乎不记得确切的语法。

I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList, then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax.

推荐答案

http://blog.firdau.si/2010/07/01/java-tips-initializing-collection/

List<String> s = Arrays.asList("1", "2");

这篇关于用于实例化初始化集合的紧凑语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆