连接多个 arraylist 的字符串内容 VALUES 。奇怪的问题

发布于 2024-12-05 19:41:24 字数 939 浏览 6 评论 0原文

我有许多具有 String 对象的 Arraylists ,并且我需要连接这些值。
例如:

ArrayList finalList = new ArrayList();
ArrayList catMe = new ArrayList();
ArrayList x = new ArrayList();
x.add("Green");
x.add("Red");
ArrayList y = new ArrayList();
y.add(" Apple");

//......

catMe.add(x);
catMe.add(y);

concatContents(catMe);  // Here i need to do 
                        // some concatenation magic.

所以当打印 finalList 时:

finalList.get(0) // should show > "Green Apple"
finalList.get(1) // should show >  "Red Apple"

我知道如果只有两个列表 X 和 Y,看起来很容易......但我需要它的 n 维。假设有第三个列表

ArrayList z= new ArrayList();
z.add(" USA");
z.add(" Canada");
catMe.add(z);
concatContents(catMe);

现在 FinalList 应该显示

Green Apple USA
Green Apple Canada
Red Apple USA
Red Apple Canada

我需要递归吗?但无法思考如何实施!有java高手有解决办法吗?

I have many Arraylists having String objects , and I have a requirement to concatenate there values.
Eg:

ArrayList finalList = new ArrayList();
ArrayList catMe = new ArrayList();
ArrayList x = new ArrayList();
x.add("Green");
x.add("Red");
ArrayList y = new ArrayList();
y.add(" Apple");

//......

catMe.add(x);
catMe.add(y);

concatContents(catMe);  // Here i need to do 
                        // some concatenation magic.

so when finalList is printed:

finalList.get(0) // should show > "Green Apple"
finalList.get(1) // should show >  "Red Apple"

I know it looks easy if there are only two list X and Y... but I need it for n dimensions. Say if there is 3rd list

ArrayList z= new ArrayList();
z.add(" USA");
z.add(" Canada");
catMe.add(z);
concatContents(catMe);

Now finalList should show

Green Apple USA
Green Apple Canada
Red Apple USA
Red Apple Canada

Do i need recursion? Unable to think how to implement though! Do any java master there have a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

他夏了夏天 2024-12-12 19:41:24

这是一个递归答案。只是煮熟了,所以质量没有保证......:)

public ArrayList<String> concatLists(ArrayList<ArrayList<String>> list) {
    ArrayList<String> catStrs = new ArrayList<String>();
    int len = list.size();
    if (len == 1) {
        catStrs.addAll(list.get(0));
        return catStrs;
    }
    ArrayList<String> myStrs = list.get(0);
    ArrayList<ArrayList<String>> strs = new ArrayList<ArrayList<String>>();
    strs.addAll(list.subList(1, len));
    ArrayList<String> retStrs = concatLists(strs);
    for (String str : myStrs) {
        for (String retStr : retStrs) {
            catStrs.add(str+retStr);
        }
    }
    return catStrs;
}

Here's a recursive answer. Just cooked it up, so no guarantees on quality... :)

public ArrayList<String> concatLists(ArrayList<ArrayList<String>> list) {
    ArrayList<String> catStrs = new ArrayList<String>();
    int len = list.size();
    if (len == 1) {
        catStrs.addAll(list.get(0));
        return catStrs;
    }
    ArrayList<String> myStrs = list.get(0);
    ArrayList<ArrayList<String>> strs = new ArrayList<ArrayList<String>>();
    strs.addAll(list.subList(1, len));
    ArrayList<String> retStrs = concatLists(strs);
    for (String str : myStrs) {
        for (String retStr : retStrs) {
            catStrs.add(str+retStr);
        }
    }
    return catStrs;
}
只涨不跌 2024-12-12 19:41:24

像这样的东西应该有效。 (我实际上并没有编译它,为了简单起见,将其写为伪代码。照顾泛型和正确的类型列表 List>)

List<ArrayList> lists;  // add all your lists to this list
ArrayList<String> final_list; // your final list of concatenations

for (int i=0; i<list1.size(); i++) {
    String temp = ""
    for (ArrayList current_list : lists) {
       temp += " " +current_list.get(i);
    }
    final_list.add(temp);
}

编辑 - 好吧,上面的代码有点愚蠢,我没有正确理解这个问题。现在,随着其他人发布了递归解决方案,我认为通过发布非递归工作解决方案会得到回报。所以这是完全按照预期工作的

public static void main(String[] args) {
    ArrayList<String> finalList = new ArrayList<String>();
    ArrayList<String> x = new ArrayList<String>();
    x.add("Green");
    x.add("Red");
    ArrayList<String> y = new ArrayList<String>();
    y.add(" Apple");
    ArrayList<String> z = new ArrayList<String>();
    z.add(" USA");
    z.add(" Canada");
    finalList = concat(x, y, z);
    System.out.println(finalList);
}

static ArrayList<String> concat(ArrayList<String>... lists) {
    ArrayList<String> result = new ArrayList<String>();
    for (ArrayList<String> list : lists) {
        result = multiply(result, list);
    }
    return result;
}

static ArrayList<String> multiply(ArrayList<String> list1, ArrayList<String> list2) {
    if (list2.isEmpty()) { return list1; }
    if (list1.isEmpty()) { return list2; }

    ArrayList<String> result = new ArrayList<String>();
    for (String item2 : list2) {
        for (String item1 : list1) {
            result.add(item1 + item2);
        }
    }
    return result;
}

something like this should work. (I did not actually compile this, wrote it as sorta pseudo code for simplicity. take care of generics and proper types list List>)

List<ArrayList> lists;  // add all your lists to this list
ArrayList<String> final_list; // your final list of concatenations

for (int i=0; i<list1.size(); i++) {
    String temp = ""
    for (ArrayList current_list : lists) {
       temp += " " +current_list.get(i);
    }
    final_list.add(temp);
}

EDIT -- okay so the code above was bit stupid, i had not understood the question correctly. Now as others have posted the recursive solutions, I thought would pay off by posting a non recursive working solution. So here is the one that works exactly as expected

public static void main(String[] args) {
    ArrayList<String> finalList = new ArrayList<String>();
    ArrayList<String> x = new ArrayList<String>();
    x.add("Green");
    x.add("Red");
    ArrayList<String> y = new ArrayList<String>();
    y.add(" Apple");
    ArrayList<String> z = new ArrayList<String>();
    z.add(" USA");
    z.add(" Canada");
    finalList = concat(x, y, z);
    System.out.println(finalList);
}

static ArrayList<String> concat(ArrayList<String>... lists) {
    ArrayList<String> result = new ArrayList<String>();
    for (ArrayList<String> list : lists) {
        result = multiply(result, list);
    }
    return result;
}

static ArrayList<String> multiply(ArrayList<String> list1, ArrayList<String> list2) {
    if (list2.isEmpty()) { return list1; }
    if (list1.isEmpty()) { return list2; }

    ArrayList<String> result = new ArrayList<String>();
    for (String item2 : list2) {
        for (String item1 : list1) {
            result.add(item1 + item2);
        }
    }
    return result;
}
执着的年纪 2024-12-12 19:41:24

快速而肮脏的解决方案:

public class Lists {
    public static void main(String[] args) {
        List<List<String>> finalList = new ArrayList<List<String>>();

        List<String> x = new ArrayList<String>();
        x.add("Green");
        x.add("Red");
        x.add("Purple");

        List<String> y = new ArrayList<String>();
        y.add("Apple");

        List<String> z = new ArrayList<String>();
        z.add("USA");
        z.add("UK");
        z.add("France");

        finalList.add(x);
        finalList.add(y);
        finalList.add(z);

        for (String s: concat(finalList)) {
            System.out.println(s);
        }
    }

    private static List<String> concat(List<List<String>> inputList) {
        if (inputList.size() == 1) {
            return inputList.get(0);
        } else {
            List<String> newList = new ArrayList<String>();
            List<String> prefixes = inputList.get(0);
            for (String prefix : prefixes) {
                for (String concat : concat(inputList.subList(1,inputList.size()))) {
                    newList.add(prefix + " " + concat);
                }
            }

            return newList;
        }
    }
}

给出:

Green Apple USA
Green Apple UK
Green Apple France
Red Apple USA
Red Apple UK
Red Apple France
Purple Apple USA
Purple Apple UK
Purple Apple France

Quick and dirty solution:

public class Lists {
    public static void main(String[] args) {
        List<List<String>> finalList = new ArrayList<List<String>>();

        List<String> x = new ArrayList<String>();
        x.add("Green");
        x.add("Red");
        x.add("Purple");

        List<String> y = new ArrayList<String>();
        y.add("Apple");

        List<String> z = new ArrayList<String>();
        z.add("USA");
        z.add("UK");
        z.add("France");

        finalList.add(x);
        finalList.add(y);
        finalList.add(z);

        for (String s: concat(finalList)) {
            System.out.println(s);
        }
    }

    private static List<String> concat(List<List<String>> inputList) {
        if (inputList.size() == 1) {
            return inputList.get(0);
        } else {
            List<String> newList = new ArrayList<String>();
            List<String> prefixes = inputList.get(0);
            for (String prefix : prefixes) {
                for (String concat : concat(inputList.subList(1,inputList.size()))) {
                    newList.add(prefix + " " + concat);
                }
            }

            return newList;
        }
    }
}

gives:

Green Apple USA
Green Apple UK
Green Apple France
Red Apple USA
Red Apple UK
Red Apple France
Purple Apple USA
Purple Apple UK
Purple Apple France
简单气质女生网名 2024-12-12 19:41:24

这是我的简单实现:

List<ArrayList<String>> lists= new ArrayList<ArrayList<String>>();
        ArrayList<String> final_list=new ArrayList<String>();;
            int i=0;
        while(true){
            StringBuilder temp = new StringBuilder();

           for(ArrayList<String> currentList:lists){
            if(i<currentList.size()){
                temp.append(currentList.get(i));
            }
           }
           String row = temp.toString();
           if(row.length()==0){
               break;

           } else{
               final_list.add(row);
               i++;
           }
        }

Here is my simple implementation:

List<ArrayList<String>> lists= new ArrayList<ArrayList<String>>();
        ArrayList<String> final_list=new ArrayList<String>();;
            int i=0;
        while(true){
            StringBuilder temp = new StringBuilder();

           for(ArrayList<String> currentList:lists){
            if(i<currentList.size()){
                temp.append(currentList.get(i));
            }
           }
           String row = temp.toString();
           if(row.length()==0){
               break;

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