简体   繁体   English

将两个arraylists加入一个

[英]adding two arraylists into one

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, 我想检查branchList是否有相同的元素,如果相同,将branchList和tglList元素分开arraylist并将该arraylist放入另一个arraylist中,

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. 我想要的结果是BranchList1有2个arraylist,其中第一个arraylist包含元素'1',第二个arraylist包含元素'2',TglList1有2个arraylist作为元素,但我得到的是第1和第2个数组得到相同的值。

How can this be done? 如何才能做到这一点?

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;
    }

}

so expected result is as below: 所以预期的结果如下:

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

but what I get is 但我得到的是

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

How can I modify the code 我该如何修改代码

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: 我没有仔细阅读你的代码(我不太了解你的要求),但如果你想将branchListtglList的元素合并(添加元素)到TglList1 ,试试这个:

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

After that, TglList1 should contain all elements of both lists. 之后, TglList1应包含两个列表的所有元素。 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"). 如果您需要对列表进行排序,您可能希望之后调用Collections.sort(TglList1) (只需注意排序字符串可能在“2”之前放置“100”,因为“1”在词法上小于“2”) 。

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

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