从一个方法返回两个变量 [英] return two variables from one method

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

问题描述

如何正确编写以下代码?

How to write the following code correctly?

public String toString(int[] position, int xOffset, int yOffset) {
    String postn = String.format("[%d,%d]", position[0], position[1]);
    String movm = String.format("[%d,%d]", xOffset, yOffset);

    return (postn, movm);
}

出现以下错误

movm cannot be returned.


推荐答案

使用 Java 8 时你可以使用配对课程。

When using Java 8 you could make use of the Pair class.

private static Pair<String, String> foo (/* some params */) {
    final String val1 = "";  // some calculation
    final String val2 = "";  // some other calculation

    return new Pair<>(val1, val2);
}

否则只返回一个数组。

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

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