我试图从一个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


当前回答

与其返回包含这两个值的数组或使用通用的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());
}

其他回答

您必须使用集合来返回多个返回值

在您的情况下,您将代码编写为

public static List something(){
        List<Integer> list = new ArrayList<Integer>();
        int number1 = 1;
        int number2 = 2;
        list.add(number1);
        list.add(number2);
        return list;
    }

    // Main class code
    public static void main(String[] args) {
      something();
      List<Integer> numList = something();
    }

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

你也可以将可变对象作为参数发送,如果你使用方法来修改它们,那么当你从函数返回时,它们将被修改。它不能用于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");
     }
}

结果是: 念完

在我看来,最好是创建一个新类,其中构造函数是你需要的函数,例如:

public class pairReturn{
        //name your parameters:
        public int sth1;
        public double sth2;
        public pairReturn(int param){
            //place the code of your function, e.g.:
            sth1=param*5;
            sth2=param*10;
        }
    }

然后像使用函数一样使用构造函数:

pairReturn pR = new pairReturn(15);

你可以使用pR.sth1 pR.sth2作为函数的2个结果

如果你确定你只需要返回两个值,你可以实现一个泛型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();