I am using the Java 2 Platform to practice different algorithms. I am getting two errors and I think the first one is that I am passing the integer “5” from arrayList[] and getting an error. The second problem is that I am getting an error in the main method when I call binarySearch();. Do I need to put arrayList as the parameter for binarySearch(arrayList)? Any help would be appreciated.
import java.util.ArrayList;
public class BinarySearch {
int arrayList[]={1,2,3,4,5,6,7,8,9};
public static int binarySearch(int[] arrayList[],int 5)
{
int low = 0;
int high = arrayList.length - 1;
int mid = 0;
while(low<=high)
{
mid = (low+high)/2;
if(arrayList[mid].compareTo(searchObj)<0)
low = mid + 1;
else if (arrayList[mid].compareTo(searchObj)>0)
{
high = mid -1;
}
else
return mid;
}
return -1;
}
public static void main(String args[])
{
binarySearch();
}
}
At the point of declaration of the BinarySearch method you need this:
That is, you are searching for searchObj in the arrayList.
Then to call the method you provide the value for arrayList and searchObj. Like this: