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 回答