如何从Java方法返回2个值? [英] How to return 2 values from a Java method?

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

问题描述

我试图从Java方法返回2个值,但我得到这些错误。这里是我的代码:

  //方法代码
public static int something(){
int number1 = 1;
int number2 = 2;

返回number1,number2;
}

//主要方法代码
public static void main(String [] args){
something();
System.out.println(number1 + number2);

错误:

<$在主线程main中的异常java.lang.RuntimeException:不可编译的源代码 - 缺少返回语句
在assignment.Main.something(Main.java:86)
在assignment.Main.main(Main.java:53)

Java结果:1

解决方案

不是返回一个包含两个值的数组,或者使用通用的 Pair 类,请考虑创建一个代表您想要返回的结果的类,并返回该类的一个实例。给班级一个有意义的名字。这种方法比使用数组的好处是类型安全,它会让你的程序更容易理解。

注意:通用 Pair 类,正如其他一些答案中所提出的那样,它也赋予了类型安全性,但并未传达结果所代表的内容。 (不使用真正有意义的名称):

  final class MyResult {
private final int first;
private final int second;
$ b $ public MyResult(int first,int second){
this.first = first;
this.second = second;
}

public int getFirst(){
首先返回;
}

public int getSecond(){
return second;



...

public static MyResult something(){
int number1 = 1;
int number2 = 2;

返回新的MyResult(number1,number2);


public static void main(String [] args){
MyResult result = something();
System.out.println(result.getFirst()+ result.getSecond());
}


I am trying to return 2 values from a Java method but I get these errors. Here is my code:

// Method code
public static int something(){
    int number1 = 1;
    int number2 = 2;

    return number1, number2;
}

// Main method code
public static void main(String[] args) {
    something();
    System.out.println(number1 + number2);
}

Error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
    at assignment.Main.something(Main.java:86)
    at assignment.Main.main(Main.java:53)

Java Result: 1

解决方案

Instead of returning an array that contains the two values or using a generic Pair class, consider creating a class that represents the result that you want to return, and return an instance of that class. Give the class a meaningful name. The benefits of this approach over using an array are type safety and it will make your program much easier to understand.

Note: A generic Pair class, as proposed in some of the other answers here, also gives you type safety, but doesn't convey what the result represents.

Example (which doesn't use really meaningful names):

final class MyResult {
    private final int first;
    private final int second;

    public MyResult(int first, int second) {
        this.first = first;
        this.second = second;
    }

    public int getFirst() {
        return first;
    }

    public int getSecond() {
        return second;
    }
}

// ...

public static MyResult something() {
    int number1 = 1;
    int number2 = 2;

    return new MyResult(number1, number2);
}

public static void main(String[] args) {
    MyResult result = something();
    System.out.println(result.getFirst() + result.getSecond());
}

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

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