动态创建循环以遍历List& lt; String' s的列表 [英] Dynamically create loops to iterate over a List of List<String>'s

查看:42
本文介绍了动态创建循环以遍历List& lt; String' s的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List< String> List ,它是通过外部API方法调用获得的:

I have a List of List<String>'s which I get from a external API method call:

List<List<String>> outerList

我必须通过将每个列表中的字符串按照它们在外部列表中的顺序连接起来来创建唯一的键组合.

I have to create unique key combinations by concatenating strings from each list in the same order they are in the outer list.

示例:如果外部列表具有2个内部列表,请说列表1:{"A","B"}和列表2:{"C","D"}.那么可能的唯一组合将是AC,AD,BC和BD.

Example: If outer list has 2 inner lists, say list1: {"A","B"} and list2: {"C","D"}. Then possible unique combinations will be AC, AD, BC and BD.

但是问题是 outerList 的大小是动态的,它可以包含任意数量的内部列表.如果内部列表号是固定的,那么我可以编写for循环并创建组合.

But the problem is the outerList size is dynamic, it can contain any number of inner lists. If the inner list numbers are fixed then I can write for loops and create combinations.

我正在考虑使用反射,递归等方向,但到目前为止还无法解决.

I am thinking in the direction of using reflections, recursion etc but so far have not been able to solve it.

public static void main(String[] args) {

    List<List<String>> outerList = new ArrayList<List<String>>();
    List<String> list1 = new ArrayList<String>();
    list1.add("A");
    list1.add("B");
    List<String> list2 = new ArrayList<String>();

    list2.add("C");
    list2.add("D");
    outerList.add(list1);

    outerList.add(list2);

    for(String s1: list1) {
        for(String s2: list2) {
            System.out.println(s1+s2);
        }
    }
}

这里 outerList 有2个内部列表,所以我为循环显式创建了2个for循环以进行串联.但是在实时的externalList中可以有任意数量的内部列表,如何在所有内部循环中动态循环并进行串联?

Here outerList has 2 inner lists so I have created 2 for loops explicitly to iterate and concatenate. But in real-time outerList can have any number of inner lists, how to loop dynamically loop through all the inner loops and concatenate?

推荐答案

此代码对我有用:

public class Test
{  

    public static void generate(LinkedList<LinkedList<String>> outerList, String outPut) {
        LinkedList<String> list = outerList.get(0);

        for(String str : list) {
            LinkedList<LinkedList<String>> newOuter = new LinkedList<LinkedList<String>>(outerList);
            newOuter.remove(list);

            if(outerList.size() > 1) {
                generate(newOuter, outPut+str);
             } else {
               System.out.println(outPut+str);
             }
        }
    }

    public static void main(String[] args) 
    {
        LinkedList<LinkedList<String>> outerList = new LinkedList<LinkedList<String>>();

        LinkedList<String> list1 = new LinkedList<String>();
        LinkedList<String> list2 = new LinkedList<String>();

        list1.add("A");
        list1.add("B");

        list2.add("C");
        list2.add("D");

        outerList.add(list1);
        outerList.add(list2);

        Test.generate(outerList, "");
    }      
}

输出:

AC
广告
卑诗省
BD

AC
AD
BC
BD

这篇关于动态创建循环以遍历List&amp; lt; String&amp;#39; s的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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