Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i solve this generic bug
    primarykey
    data
    text
    <p>i have the following code that won't compile because of generic problems. It comes from when i try to run the code. The problem is that i must fix the generic issue but i can't spot it whether i must change something in the class StAlgo or in the method.</p> <pre><code>public class StAlgo{ //signature selection sort public &lt;T extends Comparable&lt;T&gt;&gt; int selectionSort(T[] array) { } public static &lt;T extends Comparable&lt;T&gt;&gt; T[] getRandomPermutationOfIntegers(int size) { T[] data = (T[])new Comparable[size]; for (Integer i = 0; i &lt; size; i++) { data[i] = (T)i; } // shuffle the array for (int i = 0; i &lt; size; i++) { T temp; int swap = i + (int) ((size - i) * Math.random()); temp = data[i]; data[i] = data[swap]; data[swap] = temp; } return data; } public &lt;T extends Comparable&lt;T&gt;&gt; void trySelectionSort(){ int N = 100, M = 100; for(int i= 0; i &lt; N; i++){ T[] arrayInts = (T[])new Comparable[i]; for(int j= 0; j &lt; M; i++){ arrayInts = getRandomPermutationOfIntegers(i); //Collections.shuffle(Arrays.asList(arrayInts)); selectionSort(arrayInts); } } } } //Main class has the folling code: StAlgosa s = new StAlgosa(); s.trySelectionSort(); </code></pre> <p>I get the following error:</p> <pre><code>Exception in thread "main" java.lang.Error: Unresolved compilation problem: Bound mismatch: The generic method trySelectionSort() of type StAlgosa is not applicable for the arguments (). The inferred type Comparable&lt;Comparable&lt;T&gt;&gt; is not a valid substitute for the bounded parameter &lt;T extends Comparable&lt;T&gt;&gt; </code></pre> <p>how do i fix it?</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload