Java 的隐藏特性 - ArrayList / 实例初始化器说明

发布于 2024-11-09 19:16:03 字数 474 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

走过海棠暮 2024-11-16 19:16:03
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};

我真的不推荐这种方法,因为它无缘无故地创建了一个(匿名)类。

使用:

List<Integer> numbers = Arrays.asList(1, 2);

List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(1, 2));

对于 2 个级别,您可以使用:

List<List<Integer>> numbers = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(2,3,4));

通过静态导入,您甚至可以将其减少为这样,如果您确实想要:

List<List<Integer>> numbers = asList(asList(1, 2), asList(2,3,4));
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};

I don't really recommend this approach, as it creates an (anonymous) class for no good reason.

Use either:

List<Integer> numbers = Arrays.asList(1, 2);

or

List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(1, 2));

For 2 levels, you can use:

List<List<Integer>> numbers = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(2,3,4));

With a static import you could even reduce it to this, if you really want to:

List<List<Integer>> numbers = asList(asList(1, 2), asList(2,3,4));
肥爪爪 2024-11-16 19:16:03

由于 Arrays.asList 采用可变参数,双括号初始化对于列表来说不再那么有趣,您可以这样做:

List<List<Integer>> numbers = new ArrayList<List<Integer>>();
numbers.add(Arrays.asList(1,2));
numbers.add(Arrays.asList(3,4));

或者与双括号初始化结合使用,如下所示:

List<List<Integer>> numbers = new ArrayList<List<Integer>>() {{add(Arrays.asList(1,2)); add(Arrays.asList(3,4));}};

但它是否适合一行取决于您项目的代码格式指南。这是非常冗长的,以至于替代方案开始看起来不错。就像将数字放入字符串并解析它一样。或者切换到 Scala、Groovy 或 Clojure,它们都支持文字列表语法。

Double-brace initialization got a lot less interesting for lists now that Arrays.asList takes varargs, you can do this instead:

List<List<Integer>> numbers = new ArrayList<List<Integer>>();
numbers.add(Arrays.asList(1,2));
numbers.add(Arrays.asList(3,4));

Or combine with double-brace initialization like this:

List<List<Integer>> numbers = new ArrayList<List<Integer>>() {{add(Arrays.asList(1,2)); add(Arrays.asList(3,4));}};

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.

伴梦长久 2024-11-16 19:16:03

当然,您可以对内部 ArrayLists 执行相同的操作:,尽管我认为它变得非常不可读...

    List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>()
    {
        {
            add(new ArrayList<Integer>()
            {
                {
                    add(1);
                    add(2);
                }
            });
            add(new ArrayList<Integer>()
            {
                {
                    add(1);
                    add(2);
                }
            });
        }
    };

尽管不会比传统方式更长或更差(取决于您的大括号样式!)

    List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    list1.add(1);
    list1.add(2);
    list2.add(1);
    list2.add(2);
    numbers.add(list1);
    numbers.add(list2);

Arrays.asList(1, 2 )其他答案中给出的方法似乎是最好的。

And of course you can do the same for the inner ArrayLists:, though I think it becomes pretty unreadable...

    List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>()
    {
        {
            add(new ArrayList<Integer>()
            {
                {
                    add(1);
                    add(2);
                }
            });
            add(new ArrayList<Integer>()
            {
                {
                    add(1);
                    add(2);
                }
            });
        }
    };

though not wildly longer or worse than the conventional way (depending on your brace style!)

    List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    list1.add(1);
    list1.add(2);
    list2.add(1);
    list2.add(2);
    numbers.add(list1);
    numbers.add(list2);

The Arrays.asList(1, 2) approach given in other answers seems nicest of all.

演多会厌 2024-11-16 19:16:03

我不会使用这个习惯用法来“简化”ArrayList 声明。它看起来很酷,但它不必要地创建了 ArrayList 的匿名子类,而且对于我的口味来说,它看起来太“神奇”了……您希望代码能够被下一个开发人员读取和理解。

您应该考虑使用 Google Guava 等库。

这是使用 Guava 的 Lists.newArrayList(E... elements) 实用方法:

    import com.google.common.collect.Lists;


    List<ArrayList<Integer>> numbers = Lists.newArrayList(
            Lists.newArrayList(1, 2),
            Lists.newArrayList(3, 4)
    );

或者,更好的是,仅使用左侧的 List 接口(您应该避免在变量声明中使用具体类):

    import com.google.common.collect.Lists;


    List<List<Integer>> numbers = Lists.<List<Integer>>newArrayList(
            Lists.newArrayList(1, 2),
            Lists.newArrayList(3, 4)
    );

如果您知道这些列表是不可变的,那么最好使用 ImmutableList

    import com.google.common.collect.ImmutableList;


    List<List<Integer>> numbers = ImmutableList.<List<Integer>>of(
            ImmutableList.of(1, 2),
            ImmutableList.of(3, 4)
    );

I would not use this idiom to "simplify" ArrayList declarations. It looks cool, but it needlessly creates an anonymous subclass of ArrayList, 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:

    import com.google.common.collect.Lists;


    List<ArrayList<Integer>> numbers = Lists.newArrayList(
            Lists.newArrayList(1, 2),
            Lists.newArrayList(3, 4)
    );

Or, even better, using only the List interface on the left-hand side (you should avoid using concrete classes in your variable declarations):

    import com.google.common.collect.Lists;


    List<List<Integer>> numbers = Lists.<List<Integer>>newArrayList(
            Lists.newArrayList(1, 2),
            Lists.newArrayList(3, 4)
    );

If you know these lists will be immutable, it's even better to use ImmutableList:

    import com.google.common.collect.ImmutableList;


    List<List<Integer>> numbers = ImmutableList.<List<Integer>>of(
            ImmutableList.of(1, 2),
            ImmutableList.of(3, 4)
    );
梦回旧景 2024-11-16 19:16:03

只需将 int 替换为 ArrayList 对象即可

List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>() {

            {
                add(new ArrayList<Integer>());
                add(new ArrayList<Integer>());
            }
        };

Just replace int with object of ArrayList<integer>

List<ArrayList<Integer>> numbers = new ArrayList<ArrayList<Integer>>() {

            {
                add(new ArrayList<Integer>());
                add(new ArrayList<Integer>());
            }
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文