PDA

Click to See Complete Forum and Search --> : problem with dereferencing


fundean
Feb 4th, 2004, 09:51 AM
public static boolean binarySearch (int[] data, int min,
int max, int value)
{
boolean result;
int midpoint = (min + max) / 2; // determine the midpoint

if (data[midpoint].compareTo(value) == 0)
result = true;
else if (data[midpoint].compareTo(value) > 0)
{
if (min <= midpoint - 1)
result = binarySearch(data, min, midpoint - 1, value);
else
result = false;
}
else if (midpoint + 1 <= max)
result = binarySearch(data, midpoint + 1, max, value);
else
result = false;

return result;
}//method binarySearch


this code gives me the following error:

"int cannot be dereferenced". Can anyone suggest a possible fix?

Thanks !

Dillinger4
Feb 4th, 2004, 12:36 PM
I believe you would have to get the value that you wish to test out of the array first. ie...

int[] ints = {1, 2, 3};
int x = ints[1];

CornedBee
Feb 5th, 2004, 05:08 AM
You're trying to call compareTo, which is a method of Integer, on an plain int. Not good.

Use Integer's static compareTo, and call it only once (and use tags!!!)

Better yet, directly compare the ints.
public static boolean binarySearch (int[] data, int min,
int max, int value) {
boolean result = false;
int midpoint = (min + max) / 2; // determine the midpoint
if (data[midpoint] == value) {
result = true;
} else if (data[midpoint] > value) {
if (min <= midpoint - 1) {
result = binarySearch(data, min, midpoint - 1, value);
} else {
result = false;
}
} else if (midpoint + 1 <= max) {
result = binarySearch(data, midpoint + 1, max, value);
}

return result;
}//method binarySearch