简体   繁体   English

列表的访问元素 <List <List <List <Object> &gt;&gt;&gt;标题=新的ArrayList <List <List <List <Object> &gt;&gt;&gt;();

[英]Access elements of List <List <List <List <Object> >>> titles = new ArrayList <List <List <List <Object> >>> ();

I have the following list List <List <List <List <Object> >>> titles = new ArrayList <List <List <List <Object> >>> (); 我有以下列表List <List <List <List <Object> >>> titles = new ArrayList <List <List <List <Object> >>> (); and I would like to access the elements of it but I don't know how to do that .. 我想访问它的元素,但是我不知道该怎么做..

The list has 1 element that in its turn contains 3 elements and each of those 3 elements contains 6 elements but I don't know how to access each of them. 列表中有1个元素,依次包含3个元素,而这3个元素中的每个元素都包含6个元素,但是我不知道如何访问它们。

This is the output of the arraylist when I put it in a listview: 当我将其放入列表视图时,这是arraylist的输出:

 1 element---> [[[12,"01",1,"Fallo de corriente",0,1],
 2 element---> [12,"01",2,"Nivel m\u00E1ximo (activaci\u00F3n)",0,0],
 3 element--->   [12,"01",3,"Nivel m\u00E1ximo(desactivaci\u00F3n)",0,1]]]

Within each element I have 6 elements as you can see, how can I access element 1 that has 6 elements inside and access each of them? 如您所见,在每个元素中我都有6个元素,如何访问内部包含6个元素的元素1并分别访问它们?

If you really want to use that kind of structure, you can use something like this, eg to extract title of the first item. 如果您确实想使用这种结构,则可以使用类似这样的方法,例如提取第一项的标题。

String title1 = titles.get(0).get(0).get(0).get(3).toString();

So you have four levels of hierarchy, titles list is level 1 and get(0) will take the list it contains, get(0) will take first (and only item) of level 2, get(0) will take first out of 3 items of level 3, and finally get(3) will take fourth element of level 4 which is actual title as I can see. 因此,您具有四个层次结构, titles列表为1级, get(0)将获取其包含的列表, get(0)将获取级别2的第一个(也是唯一的项目), get(0)将首先get(0)级别2 3级的3个项目,最后get(3)将采用4级的第四个元素,即我所看到的实际标题。

So your title1 should be "Fallo de corriente" 因此,您的title1应该是"Fallo de corriente"

You would probably have to revisit on how you are getting the data. 您可能必须重新考虑如何获取数据。 By having ArrayList() inside another ArrayList() inside another ArrayList() ..... is a tiring work with loops iterating throughout the list. 通过在另一个ArrayList()内的另一个ArrayList()内放置ArrayList()是一项累人的工作,其中的循环遍历整个列表。

And a nightmare for the programmer to find any error. 程序员发现任何错误的噩梦。

However, the general idea of solution would be 但是,解决方案的总体思路是

Iterating through List<Object>() 遍历List<Object>()

for(Object o: objectList){}

Iterating through List<List<Object>> 遍历List<List<Object>>

    for(List<Object> outerObjList: objectListList){
       for(Object innerObject: outerObjList){
       }
    }

And so on... It's complexity grows too much for any good developer to comprehend. 依此类推...对于任何优秀的开发人员来说,它的复杂性都会增加。

I'd not prefer to write loops for List <List <List <List <Object> >>> titles ever! 我不希望为List <List <List <List <Object> >>> titles编写循环! And Ever! 永远! (Sort of a pun!). (双关语排序!)。

If there is no other go (As you are getting it from some other source), I would do write a class to perform the mapping using Jackson ObjectMapper. 如果没有其他选择(从其他来源获得它),我会写一个类来使用Jackson ObjectMapper执行映射。 Going forth, your code might be a little more readable as mentioned here 展望第四,提到你的代码可能会有点更具可读性这里

Please read of how to Structure your Classes and possibility for reducing the level of nesting in loops.Maybe re-organize your data into 2-3 individual Map ? 请阅读如何构造您的类以及减少循环嵌套的可能性。也许将您的数据重新组织为2-3个单独的 Map Happy coding! 编码愉快!

It sounds as if the storing in lists of lists is superfluous and you are only interested in the values of the inner list. 听起来好像列表列表中的存储是多余的,而您只对内部列表的值感兴趣。 If so, does flatmapping help you for further processing? 如果是这样,则映射将帮助您进行进一步处理吗?

List <Object> result = titles.stream()
                            .flatMap(x -> x.stream())
                            .flatMap(y -> y.stream())
                            .collect(Collectors.toList());
System.out.println(result);
//[[12, 01, 1, Fallo de corriente, 0, 1], [12, 01, 2, Nivel máximo (activación), 0, 0], [12, 01, 3, Nivel máximo(desactivación), 0, 1]]

Or add one more step to get a list of each value: 或再增加一个步骤以获取每个值的列表:

List <Object> result = titles.stream()
                            .flatMap(x -> x.stream())
                            .flatMap(y -> y.stream())
                            .flatMap(z -> z.stream())
                            .collect(Collectors.toList());
System.out.println(result);

//[12, 01, 1, Fallo de corriente, 0, 1, 12, 01, 2, Nivel máximo (activación), 0, 0, 12, 01, 3, Nivel máximo(desactivación), 0, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM