I want to return two objects from a Java method and was wondering what could be a good way of doing so?
The possible ways I can think of are: return a HashMap<
In C, you would do it by passing pointers to placeholders for the results as arguments:
void getShoeAndWaistSizes(int *shoeSize, int *waistSize) {
*shoeSize = 36;
*waistSize = 45;
}
...
int shoeSize, waistSize;
getShoeAndWaistSize(&shoeSize, &waistSize);
int i = shoeSize + waistSize;
Let's try something similar, in Java.
void getShoeAndWaistSizes(List shoeSize, List waistSize) {
shoeSize.add(36);
waistSize.add(45);
}
...
List shoeSize = new List<>();
List waistSize = new List<>();
getShoeAndWaistSizes(shoeSize, waistSize);
int i = shoeSize.get(0) + waistSize.get(0);