10

私はJavaクラスを書きました:

public class Tuple<X, Y> { 
  public final X x; 
  public final Y y; 
  public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
  } 
}

しかし、次のような関数を作成すると:

public Tuple<boolean, String> getResult()
{
    try {
        if(something.equals(something2))
             return new Tuple(true, null);
    }
    catch (Exception e){
        return new Tuple(false, e.getMessage());
}

ただし、次のコンパイル エラーが発生します。

unexpected type
  required: reference
  found:    boolean

私は何ができますか?

4

2 に答える 2

12

ジェネリックはプリミティブ型用ではありません。Booleanの代わりに使用しbooleanます。

public Tuple<Boolean, String> getResult() {
    //your code goes here...
}
于 2013-05-31T22:56:40.917 に答える
0

これが、Boolen、Integer、Long などのクラスが導入された理由です。Java には、プリミティブではなくオブジェクトが必要な API がいくつかあります。

于 2013-06-01T02:25:17.493 に答える