从Java方法返回元组 [英] Return Tuple from Java Method

查看:711
本文介绍了从Java方法返回元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个java类:

I wrote a java class:

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; 
  } 
}

但是当我创建这样的函数时:

but when I create a function like this:

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

但是,我收到以下编译错误:

However, I get the following compilation error:

unexpected type
  required: reference
  found:    boolean

我能做什么?

推荐答案

泛型不适用于原始类型。使用 Boolean 而不是 boolean

Generics aren't for primitive types. Use Boolean instead of boolean.

public Tuple<Boolean, String> getResult() {
    //your code goes here...
}

这篇关于从Java方法返回元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆