我需要使用广义编程返回最接近 val 的元素 c 。当我尝试编译时,我继续收到以下错误:
Selector.java:50:找不到符号符号:方法 get(int) 位置:接口 java.util.Collection 双距离 = tm.distance(c.get(0), val);
不知道如何纠正这一点。非常感谢任何帮助或提示。
import java.util.Collections;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public final class Selector {
private Selector() { }
public static <T> T nearest(Collection<T> c, T val, Telemeter<T> tm)
{
if (c == null || c.isEmpty())
{
throw new IllegalArgumentException();
}
int ix = 0;
double distance = tm.distance(c.get(0), val);
for (int i = 1; i < c.size(); i++)
{
if (cdistance <= distance)
{
ix = i;
distance = cdistance;
}
}
int theNum = c.get(ix);
return theNum;
}
}
遥测仪
import java.util.Comparator;
public interface Telemeter<E> extends Comparator<E> {
double distance(E e1, E e2);
}