we are currently learning Generics in class but I am still confused. I am currently doing an assignment and what my teacher wants me to do is to use the binary search algorithm to search for a circle with a certain radius. I believe I have everything set up but I am confused about how I can pass an array of objects to a generic method. If I can't how else can I get access an array of objects using generics. I hope I am asking this correctly
Here is what I have of course it's not done.
public class Question1
{
public static void main(String[] args)
{
Circle[] b = {new Circle(1),new Circle(2),new Circle(3),new Circle(4),new Circle(5)};
System.out.println(b[0].getR());
run(b,3,0, b.length-1);
}
/**
*
* @param <T>
* @param b
* @param key
* @param low
* @param high
*/
public static <T> Object run(T[] b , T key, int low, int high)
{
int mid = ((low + high)/2);
if(high < low)
{
System.out.print(-1);
}
if(key == b[mid])
{
System.out.println(b[mid]);
}
if(key < b[mid])
{
return run(b,key,low,mid-1);
}
else
{
return run(b,key,mid+1,high);
}
}
}
Thanks, fish