|
-
Feb 4th, 2004, 10:51 AM
#1
Thread Starter
Lively Member
problem with dereferencing
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 !
-
Feb 4th, 2004, 01:36 PM
#2
Dazed Member
I believe you would have to get the value that you wish to test out of the array first. ie...
Code:
int[] ints = {1, 2, 3};
int x = ints[1];
Last edited by Dilenger4; Feb 4th, 2004 at 01:47 PM.
-
Feb 5th, 2004, 06:08 AM
#3
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 [code][/code] tags!!!)
Better yet, directly compare the ints.
Code:
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|