How to return more than one value from a function in Java?
Can anyone give sample code for doing this using tuples?
I am not able to understand the concept of tuples.
public class Tuple{
public static void main(String []args){
System.out.println(f());
}
static Pair f(){
return new Pair("hi",3);
}
public class Pair {
public final String a;
public final Integer b;
public Pair(String a, Integer b) {
this.a = a;
this.b = b;
}
}
}
What is the mistake in the above code?
解决方案
Create a class that holds multiple values you need. In your method, return an object that's an instance of that class. Voila!
This way, you still return one object. In Java, you cannot return more than one object, whatever that may be.