-2

Here's my java code and the problem is that the use of relational operator(<) inside binarySearch() is giving error. I guess this error I am getting because the operands are of type object. How to remove this error so my function runs perfectly?

import java.util.Random;
import java.util.Arrays;
class BinarySearch
{
    public static void main(String $[])
    {



        Integer arr[]=new Integer[20];
        for(int i=0;i<20;i++)
            arr[i]=(new Random()).nextInt()%10000;

        display("Initial array :\n");
        array(arr);

        Arrays.sort(arr);
        display("After sorting :\n");
        array(arr);

        display("Enter the element to be searched for : ");
        Integer elem=(new java.util.Scanner(System.in)).nextInt();

        display(elem+(binarySearch(arr,elem)?" Found":" Not found")+"\n");

    }
    public static <T>boolean binarySearch(T arr[],T val)
    {
        int start=0;
        int end=arr.length-1;

        while(start<=end)
        {
            int mid=(start+end)/2;
            if(arr[mid]==val)
                return true;

            if(arr[mid]<val)
                start=mid+1;
            else
                end=mid-1;
        }

        return false;
    }
    public static void display(Object o)
    {
        System.out.print(o);
    }
    public static <T>void array(T arr[])
    {
        for(int i=0;i<arr.length;i++)
            display(arr[i]+" ");
        display("\n");
    }
}
Udesh
  • 2,415
  • 2
  • 22
  • 32

2 Answers2

2

The problem is that your binarySearch() method is accepting parameters that will be Objects rather than primitive types, so it is unwise to compare them using the equality operator == and invalid to compare them using the less than operator <. Instead define your binarySearch method as follows:

public static <T extends Comparable<T>> boolean binarySearch(T arr[],T val) {
    int start = 0;
    int end = arr.length-1;

    while(start <= end) {
        int mid=(start+end)/2;
        int comparison = arr[mid].compareTo(val);
        if(comparison == 0) {
            return true;
        }

        if(comparison < 0) {
            start = mid+1;
        }
        else {
            end = mid-1;
        }
    }

    return false;
}
Palamino
  • 791
  • 3
  • 10
  • T extends Comparable , Comparable is an interface how can we extend it. – Udesh May 24 '19 at 15:03
  • 1
    @DevParzival in the method definition, the java syntax does not distinguish an interface from a class. It means that the generic argument T can be treated syntactically as if T was an instance of a class that implements Comparable interface. – Palamino May 24 '19 at 15:10
  • 1
    Better would be `T extends Comparable` – Lino May 24 '19 at 15:15
  • 1
    @Lino excellent point! I have updated my answer accordingly, thanks! – Palamino May 24 '19 at 15:16
1

Read here about generics. Since all generics are objects - you can't use comparison operators with them. Even if you type <T extends Number.

There are two ways to handle this:

  1. Pass Comparator<T> to the method and use comparator.compare(arr[mid], val) for comparing values.
  2. Write <T extends Comparable> and call arr[mid].compareTo(val).

Both these methods return an integer value:

  • 0, if values are equal
  • negative, if first value less than second
  • positive, if first value greater than second
DDovzhenko
  • 1,295
  • 1
  • 15
  • 34