1

Comparable Objects を受け入れるように DataSet を変更する必要があります。テスターがコンパイルされず、compareTo メソッドを出力する方法がわかりません。テスターに​​ ArrayList を使用する必要がありますか? お早めにどうぞ!

public interface Comparable
{
/**
 Compares this object with another.
  @param other the object to be compared
  @return a negative integer, zero, or a positive integer if this object
  is less than, equal to, or greater than, other
*/
int compareTo(Object other);
}

public class DataSetComparable
{
private double sum;
private Object maximum;
private Object minimum;
private int count;
private Comparable comparer;

/**
  Constructs an empty data set with a given measurer.
  @param aMeasurer the measurer that is used to measure data values
*/
public DataSetComparable(Comparable acomparer)
{
  sum = 0;
  count = 0;
  maximum = null;
  minimum = null;
  comparer= acomparer;
}

/**
  Adds a data value to the data set.
  @param x a data value
*/
public void add(Object x)
{ 
sum = sum + comparer.compareTo(x);
  if (count == 0 || comparer.compareTo(maximum) < comparer.compareTo(x))

     maximum = x;

    if (count == 0 || comparer.compareTo(minimum) > comparer.compareTo(x))

    minimum=x;
    count++;

}


/**
  Gets the largest of the added data.
  @return the maximum or 0 if no data has been added
*/
public Object getMaximum()
{
  return maximum;
}

/**Gets the smallest of the added data.
*@return the minimum or 0 if no data has been added
**/

 public Object getMinimum()
{
  return minimum;
}
}

 public class String implements Comparable {

private String input;
private int holder;

public String(String aninput){
    input= aninput;
    holder=0;
}

public String getComparer(){
    return input;
}

public String getString(){
    return input;
}

public int compareTo(Object other){
    String temp= (String) other;
    if(input.compareTo(temp)<0){
        holder=-1;  
    }
    else if (input.compareTo(temp)== 0) {
        holder= 0;
    }
    else{
        holder= 1;
    }
    return holder;
}
 }

 public class StringTester{
public static void main (String [] args){

  Comparable c = new String();
  DataSetComparable data = new DataSetComparable(c);

  data.add(new String("Jimmy"));
  data.add(new String("Amy"));
  data.add(new String("Melissa"));
  data.add(new String("Melissa"));

  String max = (String) data.getMaximum();
  String min = (String) data.getMinimum();

  System.out.println("Maximum String = " + max);
  System.out.println("Minimum String = " + min);
}
 }

より具体的には、エラーは次のように述べています。

クラス String のコンストラクター String は、特定の型に適用できません。

4

1 に答える 1

2

あなたのコードにはこれが含まれています:

 public class String implements Comparable {
      ...
 }

Stringデフォルトですべてのクラスにインポートされる、と呼ばれる標準の Java ライブラリ クラスがあることを認識していますか? 呼び出された独自のクラスStringを実装すると、非常に紛らわしいコンパイル エラー メッセージが表示されます。

クラスの名前を別の名前に変更することを強くお勧めします。例えばStringHolder


技術的には、 というクラスを定義できることに注意してくださいString。ただし、クラスの名前を明確にするために Java が使用する規則は、このユースケース用に設計されたものではありませんjava.lang.String。使用する場所はどこでも完全修飾名で参照する必要があります。そして、あなたのコードを読んだり変更したりする他の人は、それが本当に厄介で迷惑だと思うでしょう。

java.langパッケージ内のクラスの名前を「予約済み」として扱い、同じ (修飾されていない) 名前でクラスを定義しないことが最善です。

于 2013-02-18T00:50:51.570 に答える