我在編寫一個程序,它接受類型可比的DataSet類,以便它可以找到最大值和最小值。唯一的問題是我無法編譯任何一個,而且我對如何解決錯誤有些困惑。由於編譯java程序時遇到問題
DataSet.java:
public class DataSet<T implements Comparable>
{
private T maximum;
private T least;
private int count;
public void add(T x)
{
if(count == 0){
least = x;
maximum = x;
}
else if(least.compareTo(x) > 0)
least = x;
else if(maximum.compareTo(x) < 0)
maximum = x;
count++;
}
public T getMaximum()
{
return maximum;
}
public T getLeast()
{
return least;
}
}
錯誤:
java:5: error: '(' or '[' expected
DataSet<String> ds = new DataSet<String>;
^
1 error
可比:
public interface Comparable
{
public int compareTo(Object other);
}
這裏沒有錯誤
public class DataSetTester
{
public static void main(String[] args)
{
DataSet<String> ds = new DataSet<String>;
ds.add(man);
ds.add(woman);
System.out.println("Maximum Word: " + ds.getMaximum());
}
}
錯誤:
java:5: error: '(' or '[' expected
DataSet<String> ds = new DataSet<String>;
^
感謝大家的括號,但現在我得到了更多的錯誤: – James 2012-02-26 00:49:31