12

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,

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.

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user438159
  • 497
  • 4
  • 7
  • 9
  • 13
    Please try to rephrase your question. – asgs Aug 24 '11 at 14:24
  • Can you show an example of the start data lists and what the end data lists should look like? – Yogurt The Wise Aug 24 '11 at 14:25
  • finally solve, just change newBranchList.clear() to newBranchList = new ArrayList() and newTglList.clear() to newTglList = new ArrayList() – user438159 Aug 24 '11 at 16:22
  • Should the title not be "how to merge two related arraylists based on values in the first one"? It seems that you are not "adding" anything, just rebuilding the arraylists with sublists based on the values in the first one. On a related note, using independent ArrayLists for each property is discouraged - it would be a lot clearer to use custom classes with attributes for each property, and place *those* into a single arraylist... – tucuxi Mar 15 '15 at 09:38
  • 1
    possible duplicate of [How do I join two lists in Java?](http://stackoverflow.com/questions/189559/how-do-i-join-two-lists-in-java) – Nateowami Mar 27 '15 at 07:38

1 Answers1

30

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

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").

Thomas
  • 87,414
  • 12
  • 119
  • 157