I am still new to Generics and I am wondering why I can't do "if(b[m] > key)" its probably something very simple and I'm too tired to notice. But As you can see I am trying to find a certain number in an array of integers. What am I doing wrong? It has to be a generic method.
public class Question1<T>
{
/**
* This method searches through the array of integers to check if they match the key.
* @param b array
* @param key certain number
*/
public static <T> void search(T[] b,T key)
{
int l = 0;
int r = b.length - 1;
while (l <= r)
{
int m = (l + (r-l)/2);
if(b[m].equals(key))
System.out.println("Key: " + key.toString()+ " is in Element: " + m);
if (b[m] > key)
{
l = m + 1;
}
else
{
r = m - 1;
}
}
System.out.println("Not in array.");
}
public static void main(String[] args)
{
Integer[] iray = {1,2,3,4,5};
int key = 4;
search(iray,key);
}
>.