0

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.

public class StAlgo{

//signature selection sort
public <T extends Comparable<T>> int selectionSort(T[] array) {
}

 public static <T extends Comparable<T>> T[] getRandomPermutationOfIntegers(int size) {
      T[] data = (T[])new Comparable[size]; 
      for (Integer i = 0; i < size; i++) {
          data[i] = (T)i;
      }

      // shuffle the array
      for (int i = 0; i < 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 <T extends Comparable<T>> void trySelectionSort(){
      int N = 100, M = 100;  

      for(int i= 0; i < N; i++){
          T[] arrayInts = (T[])new Comparable[i];
          for(int j= 0; j < M; i++){
              arrayInts = getRandomPermutationOfIntegers(i);
              //Collections.shuffle(Arrays.asList(arrayInts));
              selectionSort(arrayInts);
          }
      }
  }
}







//Main class  has the folling code:

    StAlgosa s = new StAlgosa();
    s.trySelectionSort();

I get the following error:

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<Comparable<T>> is not a valid substitute for the bounded parameter <T extends Comparable<T>>

how do i fix it?

thanks

Eddy Freeman
  • 3,207
  • 6
  • 35
  • 55
  • 1
    It would be useful to give out the stack trace. – Pavan Nov 01 '11 at 11:18
  • No, not really. The "unresolved compilation problem" exception means this class just didn't compile, but he told Eclipse to run the program anyway. The line numbers wouldn't be meaningful. – Ernest Friedman-Hill Nov 01 '11 at 11:53

2 Answers2

1

Partial fix is to have something like:

public class StAlgo<T extends Comparable<T>>

However, you will still have problem at

data[i] = (T) i;

because you are making Integers in that loop but your T type might not be implicitly assignable...

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
0

This looks like a problem with Type Erasure - http://download.oracle.com/javase/tutorial/java/generics/erasure.html

When you do T extends Foo<T>, after compilation, Java just remembers T extends Foo.

Checkout Generic Restriction Hell: Bound Mismatch post for more information.

Community
  • 1
  • 1
Pavan
  • 1,245
  • 7
  • 10
  • thanks for your reply. I can't compile it to print the stack trace – Eddy Freeman Nov 01 '11 at 11:21
  • Oh. I thought you were getting an exception. Its the compilation process that's throwing up is it? My bad. Can you, then provide the line where the compiler is failing? That will help fix the exact issue – Pavan Nov 01 '11 at 11:30
  • this line : s.trySelectionSort(); – Eddy Freeman Nov 01 '11 at 11:32
  • When I use this code, I get 2 errors - You have named the Class wrong i.e. StAlgosa and you are trying to assign data[i] = (T) i. These 2 are the only issues. I actually do not get an error otherwise at the place you have mentioned. – Pavan Nov 01 '11 at 11:49
  • I see. The class name is not an issue with me. I made the mistake when copying to the text editor. My class names are intact. How do i fix data[i] = (T) i; ? – Eddy Freeman Nov 01 '11 at 11:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4636/discussion-between-pavan-sudarshan-and-eddy-freeman) – Pavan Nov 01 '11 at 12:00
  • You are trying to generate an array of random elements of type T. You would not be able to do that right? Since you do not know how to instantiate them. – Pavan Nov 01 '11 at 12:02