简体   繁体   English

由两个列表的组合形成新的小列表

[英]forming new small lists from a combination set of two lists

I have two patterns of lists inside a big list. 我在大列表中有两种列表模式。

[[5.35, 5.09, 4.95, 4.81, 4.75, 5.19], [3601.0, 3602.0, 3603.0, 3600.0, 3610.0, 3600.0],[..,..,..,],[..,..,..],...]

To put in simple words, it is a combination of 简而言之,它是

[ [pricesList1], [DurationList1], [PricesList2], [DurationList2],... ] [[pricesList1],[DurationList1],[PricesList2],[DurationList2],...]

I now want to create a new list with the price and corresponding duration from both lists as a pair from each set. 现在,我想创建一个新列表,其中两个列表中的价格和对应的持续时间成对出现。 For Example : 例如 :

[[[5.35,3601.0],[5.09,3602.0],[4.95,3603],[4.81,3600],[4.75,3610],....],[[p1,d1],[p2,d2],[p3,d3],..],[[],[],[],..],....]

I have tried using List<List<Object>> and List<List<String>> . 我尝试使用List<List<Object>>List<List<String>> But no use. 但是没用。 How can I do this? 我怎样才能做到这一点?

I programed as following, which is wrong : 我编程如下,这是错误的:

List<List<Object>> DurationList = new ArrayList<List<Object>>();
List<List<Object>> FinalList = new ArrayList<List<Object>>();
List<List<String>> SlotList = null;
for(int pair=0; pair<(FinalList.size()-1) ; pair=pair+2)
                {
                    for(int innerloop=0; innerloop<(FinalList.get(pair).size());innerloop++)
                            {
                            SlotList = new ArrayList<List<String>>();
                            SlotList.addAll((Collection<? extends List<String>>) (FinalList.get(pair).get(innerloop)));
                            }
                }
for(int pair=1; pair<(FinalList.size()) ; pair=pair+2)
                {
                    for(int innerloop=0; innerloop<(FinalList.get(pair).size());innerloop++)
                            {
                            SlotList.addAll((Collection<? extends List<Object>>) FinalList.get(pair).get(innerloop));
                            }
                }

Assuming the input list always has an even number of sublists and pairs of sublists have the same size, you can use a for loop iterating over the outer lists's element two by two : 假设输入列表始终具有偶数个子列表,并且子列表对具有相同的大小,则可以使用for循环对外部列表的元素进行两个两个的迭代:

List<List<String>> result = new ArrayList<>();
for (int i=0; i<outerList.size(); i+=2) {
    List<String> priceList = outerList.get(i);
    List<String> durationsList = outerList.get(i+1);
    for (int j=0; j<priceList.size(); j++) {
        List<String> newEntry = new ArrayList<>();
        newEntry.add(priceList.get(j));
        newEntry.add(durationsList.get(j));
        result.add(newEntry);
    }
}

As commented I suggest defining your own class to store the price and duration rather than using that List<String> newEntry . 如前所述,我建议定义您自己的类来存储价格和持续时间,而不是使用List<String> newEntry

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

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