Java two varargs in one method

后端 未结 12 2233
小鲜肉
小鲜肉 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 21:58

    follwing on Lemuel Adane (cant comment on the post, due to lack of rep :))

    if you use

    public void f(Object... args){}
    

    then you may loop using How to determine an object's class (in Java)?

    like for instance

    {
       int i = 0;
       while(i< args.length && args[i] instanceof String){
             System.out.println((String) args[i]);
             i++ ;
       }
       int sum = 0;
       while(i< args.length){
             sum += (int) args[i];
             i++ ;
       }
       System.out.println(sum);
    }
    

    or anything you intend to do.

提交回复
热议问题