title: How to repeatedly add to ArrayList<ArrayList<String>> using a loop and only one other ArrayList<String>?
body: <p>I'm new to java, and I want to have an ArrayList (mainList) of ArrayLists of Strings, with an unknown number sets of strings that can only be extracted as individual strings, not all together as a list.</p>
  
  <p>My idea was to repeatedly append each string of a set to an ArrayList (list2), append list2 to the end of mainList, then empty the ArrayList, then append each string of the next set to list2, append that one, empty the list, repeat. I've included an example of what I tried to execute: </p>
  
  <pre><code>    private ArrayList&lt;ArrayList&lt;String&gt;&gt; mainList = new ArrayList&lt;&gt;();
  
      private ArrayList&lt;String&gt; list2 = new ArrayList&lt;&gt;();
  
  
  
  for (int i = 0; i &lt; items.size(); i++){
  
       for (int j = 0; j &lt; items.get(i).innerItem.size(); j++){
  
            list2.add(items.get(i).innerItem.get(j).string);
       }
  
       mainList.add(list2);
  
       list2.clear();
  }
  </code></pre>
  
  <p>let's say that there are 2 items, having innerItem 1 with strings "haha" and "hehe", and innerItem2 with strings "chuckle" and "what"</p>
  
  <p>mainList then has two lists in it, both of which are "chuckle" and what"</p>
  
  <p>The mainList ends up having multiple lists of all the same array: the last list that was appended. The Clear function seems to clear both the list2 and the mainList section that was appended. How do I make it so that the lists stay different? Thanks in advance!</p>