title: Sorting an array of Integers with generics (Java)
body: <p>I'm a second year computer science student currently working in Java and we recently started generics. I have an assignment where I've been given a list of sorting algorithms that use generics and am tasked with using them to sort a list of Integers (not primitive ints). As the sort classes use generics that extend Comparable I thought there would be no problems simply handing them the Integer array but the build output keeps coming up with incompatible types.</p>
  
  <p>The relevant code is below;</p>
  
  <p>portion of Main program</p>
  
  <pre><code>final int NUMITEMS = 100000;
  Integer[] list = new Integer[NUMITEMS];
  int dataSize = 0;
  
  //method reads contents of a file into array and returns number of objects
  System.out.println((dataSize = readDataFile(list)));
  
  SelectionSort SS = new SelectionSort(list, dataSize);//problem is here
  </code></pre>
  
  <p>And the SelectionSort algorithm which is provided and expected to be used as-is</p>
  
  <pre><code>class SelectionSort&lt;T extends Comparable&lt;? super T&gt;&gt; implements SortAlgorithm&lt;T&gt;  {
  
  public void  sort ( T [ ] theArray,   int size ) {
  
    for (int last = size-1; last &gt; 0 ; last--)
    {
       int largest = 0;
       for (int scan = 1; scan &lt;= last; scan++)
          if (theArray[scan].compareTo(theArray[largest])&gt;0)
             largest = scan;
  
       /** Swap the values */
       T temp = theArray[largest];
       theArray[largest] = theArray[last];
       theArray[last] = temp;
    }
  } // method selectionSort
  </code></pre>
  
  <p>The problem I'm having is in declaring SelectionSort, which returns an error that the constructor cannot be applied to the given type. From what I've read in my searches here and elsewhere this kind of problem is usually encountered when using ints, but I don't understand why it isn't working with Integers.
  Any insight on the problem would be greatly appreciated as I'm still coming to terms with the concept of generics.
  Many thanks in advance!</p>