我试图从一个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中只能返回一个值,所以最简洁的方法是这样的:
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();
与其返回包含这两个值的数组或使用通用的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());
}
您不需要创建自己的类来返回两个不同的值。就像这样使用HashMap:
private HashMap<Toy, GameLevel> getToyAndLevelOfSpatial(Spatial spatial)
{
Toy toyWithSpatial = firstValue;
GameLevel levelToyFound = secondValue;
HashMap<Toy,GameLevel> hm=new HashMap<>();
hm.put(toyWithSpatial, levelToyFound);
return hm;
}
private void findStuff()
{
HashMap<Toy, GameLevel> hm = getToyAndLevelOfSpatial(spatial);
Toy firstValue = hm.keySet().iterator().next();
GameLevel secondValue = hm.get(firstValue);
}
甚至还有类型安全的好处。
在我看来,最好是创建一个新类,其中构造函数是你需要的函数,例如:
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个结果
我很好奇为什么没有人提出更优雅的回调解决方案。所以不是使用返回类型,而是使用传递给方法的处理程序作为参数。下面的例子有两种截然不同的方法。我知道这两件事对我来说哪一件更优雅。:-)
public class DiceExample {
public interface Pair<T1, T2> {
T1 getLeft();
T2 getRight();
}
private Pair<Integer, Integer> rollDiceWithReturnType() {
double dice1 = (Math.random() * 6);
double dice2 = (Math.random() * 6);
return new Pair<Integer, Integer>() {
@Override
public Integer getLeft() {
return (int) Math.ceil(dice1);
}
@Override
public Integer getRight() {
return (int) Math.ceil(dice2);
}
};
}
@FunctionalInterface
public interface ResultHandler {
void handleDice(int ceil, int ceil2);
}
private void rollDiceWithResultHandler(ResultHandler resultHandler) {
double dice1 = (Math.random() * 6);
double dice2 = (Math.random() * 6);
resultHandler.handleDice((int) Math.ceil(dice1), (int) Math.ceil(dice2));
}
public static void main(String[] args) {
DiceExample object = new DiceExample();
Pair<Integer, Integer> result = object.rollDiceWithReturnType();
System.out.println("Dice 1: " + result.getLeft());
System.out.println("Dice 2: " + result.getRight());
object.rollDiceWithResultHandler((dice1, dice2) -> {
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
});
}
}