I'm trying to create a class that reads in a set of numbers from a file, stores them in a generic array, and then sorts them using the generic bubbleSort method. I usually write in C++ and the switch from templates to generics hasn't been easy. I'm fairly certain my bubbleSort method is correct, but I'm not sure what exactly is causing the problem. Thanks in advance for the help. Here is the error message:
Bound mismatch: The generic method bubbleSort(T[], int) of type MySort<T> is not applicable for the arguments (T[], int). The inferred type T is not a valid substitute
for the bounded parameter <T extends Number & Comparable<T>>
import java.util.*;
import java.io.*;
public class MySort<T>{
/**
* @param args
*/
public static <T extends Object> void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
T array[] = (T[]) new Object[80];
try {
FileReader fr = new FileReader("tester.txt");
Scanner input = new Scanner(fr);
while(input.hasNext()){
array[count] = (T) input.nextLine();
count++;
}
bubbleSort(array, count); // the method call is where the error occurs
}
catch (FileNotFoundException e) {
System.out.println("File not Found");
}
}
public static <T extends Number & Comparable<T>> void bubbleSort(T[] array, int size) {
int swap;
do{
swap = 0;
for (int i = 0; i < size-1; i++) {
if((array[i]).compareTo(array[i+1]) > 0) {
T temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
swap++;
}
}
}
while(swap != 0);
}
}