Java two varargs in one method

后端 未结 12 2169
小鲜肉
小鲜肉 2020-11-27 21:08

Is there any way in java, to create a method, which is expecting two different varargs? I know, with the same object kind it isn\'t possible because the compiler does\'nt kn

12条回答
  •  北海茫月
    2020-11-27 22:04

    You can convert your varargs to arrays

    public void doSomething(String[] s, int[] i) {
        ...
    }
    

    then with some helper methods to convert your varargs to array like this:

    public static int[] intsAsArray(int... ints) {
        return ints;
    }
    
    public static  T[] asArray(T... ts) {
        return ts;
    }
    

    Then you can use those helper methods to convert your vararged parameters.

    doSomething(asArray("a", "b", "c", "d"), intsAsArray(1, 2, 3));
    

提交回复
热议问题