Return different type of data from a method in java?

前端 未结 13 1515
别那么骄傲
别那么骄傲 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:16

    I know this is late but I thought it'd be helpful to someone who'll come searching for an answer to this. You can use a Bundle to return multiple datatype values without creating another method. I tried it and worked perfectly.

    In Your MainActivity where you call the method:

    Bundle myBundle = method();
    String myString = myBundle.getString("myS");
    String myInt = myBundle.getInt("myI");
    

    Method:

    public Bundle method() {
        mBundle = new Bundle();
        String typicalString = "This is String";
        Int typicalInt = 1;
        mBundle.putString("myS", typicalString);
        mBundle.putInt("myI", typicalInt);
        return mBundle;
    }
    

    P.S: I'm not sure if it's OK to implement a Bundle like this, but for me, it worked out perfectly.

提交回复
热议问题