title: Spliting ArrayList into 2D ArrayList
body: <p>I have the following code:</p>
  
  <pre><code>for (int i = 0; i &lt; costs.size(); i++){
          System.out.println(twoDArrayList);
          for (int j = 0; j &lt; cities.size(); j++){
              if (i==0 || i%cities.size() != 0){
                  twoDArrayList.get(j).add(costs.get(i));
              } 
          }
      }
  </code></pre>
  
  <p>It is supposed to split an ArrayList (costs) and put values into a 2D ArrayList (twoDArrayList). This almost works except the code just puts the same values into each ArrayList within twoDArrayList. Here is the output: </p>
  
  <pre><code>begin execution
  [Seattle, NewOrleans, LosAngeles, Tucson, Chicago, Miami, Omaha]
  [[], [], [], [], [], [], []]
  [[0], [0], [0], [0], [0], [0], [0]]
  [[0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706]]
  [[0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136],        [0, 2706, 1136], [0, 2706, 1136]]
  ...etc.
  </code></pre>
  
  <p>I need it to move so that the arrayList is :</p>
  
  <pre><code>[0][2706][1136][etc...]
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Here is the full code:</p>
  
  <pre><code>private static ArrayList&lt;String&gt; cities;
  private static ArrayList&lt;Integer&gt; costs;
  private static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; twoDArrayList;
  
  
  public TSP(Scanner inFile) {
      cities = new ArrayList&lt;String&gt;();
      costs = new ArrayList&lt;Integer&gt;();
      String newCities;
      newCities = (inFile.nextLine());
      String[] stringArray = newCities.split(" ");
      for (int i = 0; i &lt; stringArray.length; i++){
          cities.add(stringArray[i]);
      }
      System.out.println(cities);
  
  
      twoDArrayList = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;();
  
  
  
      for (int i = 0; i &lt; cities.size(); i++){
          twoDArrayList.add(new ArrayList&lt;Integer&gt;()); 
      }
  
  
      while (inFile.hasNextInt()){
          int newCost = inFile.nextInt();
          costs.add(newCost);
      }
  
      for (int i = 0; i &lt; costs.size(); i++){
          System.out.println(twoDArrayList);
          for (int j = 0; j &lt; cities.size(); j++){
              if (i==0 || i%cities.size() != 0){
                  twoDArrayList.get(j).add(costs.get(i));
              } 
          }
      }
  
      System.out.println(twoDArrayList.get(0).get(0));
      System.out.println(twoDArrayList.get(0).get(1));
      System.out.println(twoDArrayList.get(0).get(2));
      System.out.println(twoDArrayList.get(2).get(0));
      System.out.println(twoDArrayList.get(2).get(1));
      System.out.println(twoDArrayList.get(2).get(2));
  
  }
  }
  </code></pre>