How to return multiple objects from a Java method?

前端 未结 25 3446
眼角桃花
眼角桃花 2020-11-21 23:55

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<

25条回答
  •  没有蜡笔的小新
    2020-11-22 00:40

    public class MultipleReturnValues {
    
        public MultipleReturnValues() {
        }
    
        public static void functionWithSeveralReturnValues(final String[] returnValues) {
            returnValues[0] = "return value 1";
            returnValues[1] = "return value 2";
        }
    
        public static void main(String[] args) {
            String[] returnValues = new String[2];
            functionWithSeveralReturnValues(returnValues);
            System.out.println("returnValues[0] = " + returnValues[0]);
            System.out.println("returnValues[1] = " + returnValues[1]);
        }
    
    }
    

提交回复
热议问题