2

I'm trying to use the binarySearch method documented at the Java API Specification but my IDE, Eclipse (Helios), is not recognizing the signature.

My class, boiled down to its 2 data members and the method in which I'm trying to call the Arrays.binarySearch:

import java.util.Arrays; // Access Arrays class
public class SortedStringArrayList {
    // member data
    private String[] items;
    private int size;

    // methods
    public int testBinSearch(String item) {
        int index = Arrays.binarySearch(items, 0, size, item);
    }
}

When I code in the method, Eclipse assumes I want a different signature and tells me:

The method binarySearch(int[], int) in the type Arrays is not applicable for the arguments (String[], int, int, String)

The signatures for binarySearch it suggested as available were:

I'm very new to Java/Eclipse. Anyone know what the problem is?

0

1 Answer 1

5

You need to tell Eclipse to use Java 1.6 (under project settings). I'm guessing you're on 1.5. 1.5 and older versions only have the basic version of binarySearch, with no fromIndex or toIndex.

If you want users with older JREs to be able to run your program, you could copy the binarySearch implementations from the 1.6 source and paste it into your own code.

Sign up to request clarification or add additional context in comments.

3 Comments

This sounds entirely plausible. 1.5 to 1.6 didn't change a ton, but where it did, reading the wrong docs is a major gotcha.
The Javadocs do say Since: 1.6, but it's subtle and easy to overlook.
Thanks all! I was indeed on 1.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.