title: Adding Integers to ArrayList<Integer>
body: <p>I have an <code>ArrayList</code> of <code>LinkedList</code>s (an array of linked lists). The <code>LinkedList</code>s contains integers (<code>Integer</code>).</p>
  
  <pre><code>private List&lt;LinkedList&gt; buckets;
  buckets = new ArrayList&lt;LinkedList&gt;();
  for (int i = 0; i &lt; 10; i++) {
      LinkedList&lt;Integer&gt; temp = new LinkedList&lt;Integer&gt;();
      buckets.add(temp);
  }
  </code></pre>
  
  <p>I later want to remove the items from the linked list (in the order they were added) and add them to an array list. When I try this:</p>
  
  <pre><code>ArrayList&lt;Integer&gt; sorted = new ArrayList&lt;Integer&gt;(unsorted.size());
  for (int i = 0; i &lt; buckets.size(); i++) {
      for (int j = 0; j &lt; buckets.get(i).size(); j++) {
          sorted.add(buckets.get(j).removeLast());
          // sorted.add((Integer)buckets.get(j).removeLast());
      }
  }
  </code></pre>
  
  <p>I get an error saying:</p>
  
  <pre><code>add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.Object)
  </code></pre>
  
  <p>But when I cast it to an <code>Integer</code> (the commented out line), the array is full of <code>null values</code>. Anyone see what I am doing wrong?</p>
  
  <p>Here is where I am adding items to <code>bucket</code>:</p>
  
  <pre><code>for (int i = 0; i &lt; unsorted.size(); i++) {
      int digit = (unsorted.get(i) / position) % 10;
      buckets.get(digit).add(unsorted.get(i));
  }
  </code></pre>
  
  <p>Note that <code>sorted</code> is an <code>ArrayList&lt;Integer&gt;</code>. When I trace it in debug mode, I can see that the LinkedLists have Integer objects with the correct values.</p>
  
  <p>Screenshot of buckets contents:</p>
  
  <p><img src="https://i.stack.imgur.com/9L0LJ.png" alt="Bucket contents debug output"></p>
  
  <p>Working Example:</p>
  
  <pre><code>class Ideone {
      private static List&lt;LinkedList&lt;Integer&gt;&gt; buckets;
      public static void main (String[] args) throws Exception {
          ArrayList&lt;Integer&gt; arr = new ArrayList&lt;Integer&gt;();
          arr.add(6);
          arr.add(8);
          arr.add(1);
          arr.add(3);
          arr.add(9);
          System.out.println(arr);
          arr = sort(arr);
          System.out.println(arr);
      }
      public static ArrayList&lt;Integer&gt; sort(ArrayList&lt;Integer&gt; unsorted) {
          buckets = new ArrayList&lt;LinkedList&lt;Integer&gt;&gt;();
          for (int i = 0; i &lt; 10; i++) {
              LinkedList&lt;Integer&gt; temp = new LinkedList&lt;Integer&gt;();
              buckets.add(temp);
          }
          ArrayList&lt;Integer&gt; sorted = new ArrayList&lt;Integer&gt;(unsorted.size());
          for (int i = 0; i &lt; unsorted.size(); i++) {
              int digit = unsorted.get(i) % 10;
              buckets.get(digit).add(unsorted.get(i));
          }
          for (int i = 0; i &lt; buckets.size(); i++) {
              for (int j = 0; j &lt; buckets.get(i).size(); j++) {
                  sorted.add(buckets.get(j).poll());
                  // sorted.add((Integer)buckets.get(j).removeLast());
              }
          }
          return sorted;
      }
  }
  </code></pre>