将两个数组列表合二为一 [英] adding two arraylists into one

查看:40
本文介绍了将两个数组列表合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查 branchList 是否有相同的元素,如果相同的 put branchList 和 tglList 元素将 arraylist 分开并将该 arraylist 放入另一个 arraylist,

I want to check branchList whether has same element or not, if same put branchList and tglList element separate arraylist and put that arraylist into another arraylist,

我想要的结果是 BranchList1 有 2 个数组列表,其中第一个数组列表包含元素1",第二个数组列表包含元素2",TglList1 有 2 个数组列表作为元素,但我得到的是第一个和第二个数组获得相同的值.

The result I want is BranchList1 have 2 arraylist where 1st arraylist contain for element '1' and 2nd arraylist contain element '2' and TglList1 have 2 arraylist as element, but what i get is both 1st and 2nd array get same value.

如何做到这一点?

ArrayList branchList = new ArrayList();
    branchList.add("1");
    branchList.add("1");
    branchList.add("1");
    branchList.add("2");
    branchList.add("2");
    branchList.add("2");

    ArrayList tglList = new ArrayList();
    tglList.add("5");
    tglList.add("10");
    tglList.add("20");
    tglList.add("100");
    tglList.add("500");
    tglList.add("1000");


    ArrayList newBranchList = new ArrayList();
    ArrayList newTglList = new ArrayList();

    ArrayList BranchList1 = new ArrayList();
    ArrayList TglList1 = new ArrayList();


    ArrayList abc = new ArrayList();
    String checkBranch = new String();

    for(int i=0;i<branchList.size();i++){
        String branch = branchList.get(i).toString();
        if(i==0 || checkBranch.equals(branch)){
            newBranchList.add(branch);
            newTglList.add(tglList.get(i).toString());
        }else{
            BranchList1.add(newBranchList);
            TglList1.add(newTglList);

            newBranchList.clear();
            newTglList.clear();

            newBranchList.add(branch);
            newTglList.add(tglList.get(i).toString());
        }
        if(i==(branchList.size()-1)){
            BranchList1.add(newBranchList);
            TglList1.add(newTglList);
        }
        checkBranch = branch;
    }

}

所以预期结果如下:

BranchList1 = [ [1,1,1],[2,2,2]]
TglList1 = [[5,10,20],[50,100,200]]

但我得到的是

BranchList1 = [ [2,2,2],[2,2,2]]
TglList1 = [[50,100,200],[50,100,200]]

如何修改代码

推荐答案

我没有通读你的代码(我不太明白你的要求),但是如果你想合并(添加branchListtglListTglList1 的元素,试试这个:

I didn't thoroughly read through your code (and I don't quite get what you're asking for), but if you want to merge (add the elements of) branchList and tglList to TglList1, try this:

TglList1.addAll(branchList);
TglList1.addAll(tglList);

之后,TglList1 应该包含两个列表的所有元素.如果您需要对列表进行排序,您可能需要在之后调用 Collections.sort(TglList1)(请注意排序字符串可能会在2"之前放置100",因为1"是在词法上小于2").

After that, TglList1 should contain all elements of both lists. If you need the list to be sorted, you might want to call Collections.sort(TglList1) afterwards (just note that sorting strings might place "100" before "2", due to "1" being lexically smaller than "2").

这篇关于将两个数组列表合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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