Can't figure out what's wrong. Operator is undefined for argument type. Error is located at the end of the binarysearch method array[position] < key
import java.util.Arrays; public class binarySearch { public static <T extends Comparable<T>> int binarysearch(T key, T[] array) { int start = 0; int end = array.length - 1; int position =-1; while (start <= end && position == -1) { int mid = start + (end - start) / 2; if (array[mid].compareTo(key) > 0) { end = position - 1; } else if (array[mid].compareTo(key) < 0) { start = position + 1; } else { position = mid; } } if (position == -1) { //if the target is not found, find the position to insert position = start; while (position < end && array[position] < key) { //error is here position ++; position --; //insert after position } } return position; } public static void main(String[] Args) { int key; Integer[] array = {2, 3, 6, 22, 23, 31, 32, 34, 41, 43}; key = 1; //integer being inserted System.out.println("\nTestcase 1\nelement inserted to the left and not already present"); System.out.println("Array : "+Arrays.toString(array)); System.out.println("Value to be inserted "+ key); System.out.println("The Key can has to be inserted at index " + binarysearch(key, array)); } }
--- Update ---
figured it out, thanks.