Return different type of data from a method in java?

前端 未结 13 1513
别那么骄傲
别那么骄傲 2020-12-05 02:18
public static void main(String args[]) {
    myMethod(); // i am calling static method from main()
 }

.

public static ? myMethod(){         


        
13条回答
  •  孤城傲影
    2020-12-05 03:15

    This can be one of the solution. But your present solution is good enough. You can also add new variables and still keep it clean, which cannot be done with present code.

    private static final int INDEX_OF_STRING_PARAM = 0;
    private static final int INDEX_OF_INT_PARAM = 1;
    
    public static Object[] myMethod() {
        Object[] values = new Object[2];
        values[INDEX_OF_STRING_PARAM] = "value";
        values[INDEX_OF_INT_PARAM] = 12;
        return values;
    }
    

提交回复
热议问题