title: adding two arraylists into one
body: <p>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, </p>
  
  <p>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.</p>
  
  <p>How can this be done?</p>
  
  <pre><code>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&lt;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;
      }
  
  }
  </code></pre>
  
  <p>so expected result is as below:</p>
  
  <pre><code>BranchList1 = [ [1,1,1],[2,2,2]]
  TglList1 = [[5,10,20],[50,100,200]]
  </code></pre>
  
  <p>but what I get is </p>
  
  <pre><code>BranchList1 = [ [2,2,2],[2,2,2]]
  TglList1 = [[50,100,200],[50,100,200]]
  </code></pre>
  
  <p>How can I modify the code</p>