title: Iterating through an array List and creating new ArrayLists when values are different, is this even possible?
body: <p>I am fairly new to Java and I have stumbled across a problem I cannot figure out for the life of me. First let me explain what I am trying to do then I will show you the code I have so far.</p>
  
  <p>I have a webservice that returns an array of arrays(which include company and lines of business strings). I wish to transform this into a string list, which I did in the first line of code below. Then I wish to Iterate through the list and every I come across a different value for company, I want to create a new ArrayList and add the associated line of business to the new list. Example output of webservice: 80,80,64,64 (this is presorted so the same companies will always be grouped together) the associated lobs would be 1,2,3,4 respectively. What I want: arraylist[0]: 1,2    arrayList[1]: 3,4</p>
  
  <p>What I have so far:</p>
  
  <pre><code>    List coList = Arrays.asList(coArray);
      //create list of lists
      List&lt;List&lt;String&gt;&gt; listOfLists = new ArrayList&lt;List&lt;String&gt;&gt;();
      String cmp = "";
      for (int i=0;i&lt;coList.size();i++){//loop over coList and find diff in companies
          String currentCo = ((__LOBList)coList.get(i)).getCompany();
          String currentLob = ((__LOBList)coList.get(i)).getLobNum();
          if(i&lt;coArray.length-1){
              String nextCo = ((__LOBList)coList.get(i+1)).getCompany();
              if((currentCo.equals(nextCo))){
                  //do nothing companies are equal
              }else{
                  log("NOT EQUAL"); //insert logic to create a new array??
                  ArrayList&lt;String&gt; newList = new ArrayList&lt;String&gt;();
          //      for(int j=0;j&lt;coList.size();j++){
                  newList.add( ((__LOBList)coList.get(i)).getLobNum());                   
          //      }   
  
  
                  for(int k=0; k&lt;listOfLists.size();k++){//loop over all lists
                      for(int l=0;l&lt;listOfLists.get(k).size();l++){ //get first list and loop through
  
                      }
                      listOfLists.add(newList);   
                  }
  
              }
          }
  
  
      }
  </code></pre>
  
  <p>My problem here is that it is not adding the elements to the new string array. It does correctly loop through coList and I put a log where the companies are not equal so I do know where I need to create a new arrayList but I cannot get it to work for the life of me, please help!</p>