增加值在Java三维名单 [英] Adding values to 3 dimensional list in Java

查看:175
本文介绍了增加值在Java三维名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

精简版

你如何在同一值添加到3维数组列表去了?

How do you add values to a 3 dimensional array list at one go?

List<List<List<String>>> FD = new ArrayList<List<List<String>>>();

我了解到,对于一个正常的ArrayList中,下面可以做

I learnt that for a normal Arraylist, the following can be done

List<String> R = new ArrayList<String>();
R.addAll(Arrays.asList("A","B","C","D","E"));

加长版

注:我将使用 - >符号,重新present决定的结果
阅读A - >乙为A判断B'。

Note: I'll be using '->' symbol to represent 'determines'.
Read A -> B as 'A determines B'.

我想捕捉某种方式如下信息。结果
A - > B,A,B - > C,B - > D,E

I am trying to capture the following information in some way.
A -> B, A,B -> C, B -> D,E

我觉得3D名单会派上用场,因为我在下面的表格把它描写

I felt a 3D list would come in handy as I pictured it in the following form

F[             --> Outermost List starts
  [            --> Middle List start (first of the three middle lists)
   [A],
   [B]
  ],            --> Middle List end
  [
   [A,B],        --> Innermost list start and end (first of the two inner lists in this middle list)
   [C]
  ],
  [
   [B],
   [D,E]
  ]
 ]             --> Outermost List ends

我选择了一个列表,因为大小是动态在一定程度上(除了中间的每一个名单将始终只有2内列出)

I chose a List since the size is dynamic to some extent (except that every middle list will always have only 2 inner lists)

我AP preciate如果你能告诉我这种填充列表的简单方法。

I would appreciate if you could show me a simple way of populating such a list.

如果你有一些替代实施建议,我开到这一点。

If you have some alternate implementation suggestions, I'm open to that too.

在此先感谢

推荐答案

筛选。我复制 Lists.newArrayList 番石榴库。

Like this. I copied Lists.newArrayList from Guava library.

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static <E> ArrayList<E> newArrayList(E... elements) {
        ArrayList<E> list = new ArrayList<>(elements.length);
        Collections.addAll(list, elements);
        return list;
    }

    public static void main(String[] args) {

        ArrayList<ArrayList<ArrayList<String>>> lists = newArrayList(
            newArrayList(
                newArrayList("A"),
                newArrayList("B")
            ),
            newArrayList(
                newArrayList("A", "B"),
                newArrayList("C")
            ),
            newArrayList(
                newArrayList("B", "D"),
                newArrayList("E")
            )
        );

        System.out.println(lists);
    }
}

这篇关于增加值在Java三维名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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