为了编译我的程序,我需要知道方法find的返回类型。我不知道如何使用以下接口IBinarySearch和类Unit:
获取类型值所以这是界面:
package u8a1;
import java.util.List;
/**
* Binary search on things with comparable keys.
*
* @param <Key>
* The type of the key of the things. Must implement the {@link Comparable} interface.
* @param <Value>
* The type of the things themselves
*/
public interface IBinarySearch<Key extends Comparable<Key>, Value> {
/**
* Performs a binary search on the vector of {@link Unit}s.
*
* @param needle
* We are looking for something whose key equals this needle with
* respect to the {@link Comparable} interface.
* @param haystack
* The vector of {@link Unit}s where we are searching for the
* needle. This vector must be sorted ascending with respect to
* the keys and the {@link Comparable} interface.
* @return A thing from the haystack whose key equals the needle or null
* if no such thing is in the haystack.
*/
public Value find(List<Unit<Key, Value>> haystack, Key needle);
}
这是班级:
package u8a1;
/**
* A key-value pair
*
* @param <Key>
* the type of the key. Must be {@link Comparable}
* @param <Value>
* the type of the value
*/
public class Unit<Key extends Comparable<Key>, Value> {
public Key key;
public Value value;
public Unit(Key key, Value value) {
this.key = key;
this.value = value;
}
}
还有另一个界面IMeasure:
package u8a1;
/**
* Support statistics of binary search algorithms
*/
public interface IMeasure {
/**
* Set the factor for the binary search.
*
* A factor of two means that the search space is split into half-half in each recursion step.
* A factor of three means that the search space is split into one third and two thirds in each recursion step.
* In each case integer division is assumed, which means fractions are rounded down.
*
* This method is called first after instantiation.
*
* @param factor
* an integer value
*/
public void setFactor(int factor);
/**
* Retrieve the statistics.
*
* @return the number of recursive calls of the binary search algorithm
* performed since instantiation.
*/
public int getNumberofCalls();
}
这是BinarySearch类,它实现了两个接口:
package u8a1;
import java.util.List;
public class BinarySearch<Key extends Comparable<Key>, Value> implements IBinarySearch<Key, Value>, IMeasure {
public Value find(List<Unit<Key, Value>> haystack, Key needle)
{
return haystack;
}
public void setFactor(int factor)
{
return;
}
public int getNumberofCalls()
{
return 0;
}
}
我想在开始实现方法之前使用正确的类型编译程序。