Results 1 to 3 of 3

Thread: problem with dereferencing

  1. #1

    Thread Starter
    Lively Member fundean's Avatar
    Join Date
    Apr 2001
    Posts
    98

    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 !

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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];

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width