java 並不支持多值的返回,但能夠利用java一些特性來模擬。實例代碼以下: java
/** * Created by wens on 15-5-23. */ public class TupleN { private Object[] values ; public TupleN(Object...values){ this.values = values ; } public <T> T getN(int index ){ if(index < 0 || index >= values.length ){ throw new IndexOutOfBoundsException(index+" of out of "+values.length) ; } return (T)values[index]; } public static void main(String[] args) { TupleN tuple = returnMultiValue(); String string = tuple.getN(0); int intV = tuple.getN(1); Bean b = tuple.getN(2) ; } public static TupleN returnMultiValue(){ return new TupleN("test string" , 44, new Bean() ) ; } static class Bean { } }