在Java中返回多个值 [英] returning multiple values in Java

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

问题描述

序言:我知道使用列表或其他集合来返回结果但是我必须通过列表来获取结果:参见第二个例子

Preamble: I am aware of using a list or other collections to return a result but then I have to go through the list plucking the results out: see 2nd example

Preamble-2:我正在寻找一个超出Java不支持的答案......的答案。

Preamble-2: I'm looking for an answer beyond "this is not supported in Java ..."

我正在寻找一种从Java方法调用中返回多个对象的便捷方法。

I'm looking for a convenient way to return multiple objects from a Java method call.

有点像PHP:

list ($obj1, $obj2, ...) foobar();

我真的厌倦了在参数中传递持有者对象,例如:

I'm really getting tired of passing holder objects in the arguments for example:

class Holder {
   int value;
}

Holder h1=new Holder();
Holder h2=new Holder();

然后:

o.foobar(h1,h2);

...如果有人想出一个优雅的方法来解决这个问题,那将非常感兴趣。

... would be very interested if someone has figured an elegant way to get round this.

使用清单

List<String> = foobar();

这有两个缺点:

我必须首先打包房子的被叫方一侧的列表:

I have to first pack the List on the callee side of the house:

// this is on the callee side of the house
ArrayList<String> result = new ArrayList<String>
result.add("foo");
result.add("bar");

然后在来电方面,我必须把结果拿出来:

Then on the caller side I have to pluck the results out:

// This is on the caller side
List<String> result = foobar();
String result1 = result.get(0);
String result2 = result.get(1); // this is not as elegant as the PHP equivalent

此外,我想要返回不同的对象types say String,Integer我必须返回一个Objects列表,然后转换每个对象......不漂亮

Further, say I wanted to return objects of different types say String, Integer I would have to return a list of Objects and then cast each object ... not pretty

谢谢。

推荐答案

使用通用元组类是我能想象到的最接近的类。为了给出一个想法,三个返回值的通用类可能如下所示:

Using a generic tuple class is the closest I can imagine. To give an idea, such general purpose class for three return values could look something like this:

public class T3<A, B, C> {
    A _1;
    B _2;
    C _3;
    T3(A _1, B _2, C _3) {
        this._1 = _1;
        this._2 = _2;
        this._3 = _3;
    }
}

这允许被叫方一个语句构造函数,例如:

This allows a one statement constructor on the callee side, for example:

public T3<String, Integer, Character> getValues() {
    return new T3<String, Integer, Character>("string", 0, 'c');
}

但是没有被调用者构建的持有者实例。

but no holder instance construction by the callee is to be done.

每个返回值都需要一个自己的元组类。 功能性Java库中可能会提供类似的内容。作为参考,这里是一个 N-Tuple实现

Each number of return values needs a tuple-class of its own. There might be something like that provided in the Functional Java library. For reference, here's an N-Tuple implementation.

对于这两个-valued case java.util.AbstractMap.SimpleEntry会这样做:

For the two-valued case java.util.AbstractMap.SimpleEntry would do:

return new SimpleEntry<String, Integer>("string", 0);

无论如何,我认为多个返回值需要考虑两个要点:

Anyhow, I see two important points to consider for multiple return values:


  1. 键入的返回值

  2. 指出每个返回值的含义

虽然很无聊,但我建议创建一个单独的类来保存所有响应值,并将其声明为方法返回类型。这样就可以突出显示每个值的含义。

As boring as it is, I'd recommend creating a separate class to hold all the response values and declare that as the method return type. That way the meaning of each value can be highlighted.

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

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