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

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

错误:

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结果:1


当前回答

首先,如果Java有元组来返回多个值会更好。

其次,编写尽可能简单的Pair类,或者使用数组。

但是,如果您确实需要返回一个pair,请考虑它所代表的概念(从字段名开始,然后是类名)—以及它所扮演的角色是否比您想象的更大,以及对它进行显式抽象是否有助于您的整体设计。也许这是一个代码提示…… 请注意:我并不是武断地说它会有帮助,但只是看看,看看它是否有用……或者如果没有。

其他回答

在Java中只能返回一个值,所以最简洁的方法是这样的:

return new Pair<Integer>(number1, number2);

这是你的代码的更新版本:

public class Scratch
{
    // Function code
    public static Pair<Integer> something() {
        int number1 = 1;
        int number2 = 2;
        return new Pair<Integer>(number1, number2);
    }

    // Main class code
    public static void main(String[] args) {
        Pair<Integer> pair = something();
        System.out.println(pair.first() + pair.second());
    }
}

class Pair<T> {
    private final T m_first;
    private final T m_second;

    public Pair(T first, T second) {
        m_first = first;
        m_second = second;
    }

    public T first() {
        return m_first;
    }

    public T second() {
        return m_second;
    }
}

如果你确定你只需要返回两个值,你可以实现一个泛型Pair:

public class Pair<U, V> {

 /**
     * The first element of this <code>Pair</code>
     */
    private U first;

    /**
     * The second element of this <code>Pair</code>
     */
    private V second;

    /**
     * Constructs a new <code>Pair</code> with the given values.
     * 
     * @param first  the first element
     * @param second the second element
     */
    public Pair(U first, V second) {

        this.first = first;
        this.second = second;
    }

//getter for first and second

然后让方法返回Pair:

public Pair<Object, Object> getSomePair();

你也可以将可变对象作为参数发送,如果你使用方法来修改它们,那么当你从函数返回时,它们将被修改。它不能用于Float之类的东西,因为它是不可变的。

public class HelloWorld{

     public static void main(String []args){
        HelloWorld world = new HelloWorld();

        world.run();
     }



    private class Dog
    {
       private String name;
       public void setName(String s)
       {
           name = s;
       }
       public String getName() { return name;}
       public Dog(String name)
       {
           setName(name);
       }
    }

    public void run()
    {
       Dog newDog = new Dog("John");
       nameThatDog(newDog);
       System.out.println(newDog.getName());
     }


     public void nameThatDog(Dog dog)
     {
         dog.setName("Rutger");
     }
}

结果是: 念完

与其返回包含这两个值的数组或使用通用的Pair类,不如考虑创建一个表示希望返回的结果的类,并返回该类的一个实例。给类一个有意义的名字。与使用数组相比,这种方法的好处是类型安全,它将使您的程序更容易理解。

注意:泛型的Pair类,正如这里其他一些回答中提出的那样,也提供了类型安全,但不传达结果所代表的内容。

示例(没有使用真正有意义的名称):

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不支持多值返回。返回一个值数组。

// Function code
public static int[] something(){
    int number1 = 1;
    int number2 = 2;
    return new int[] {number1, number2};
}

// Main class code
public static void main(String[] args) {
  int result[] = something();
  System.out.println(result[0] + result[1]);
}