Java的可变参数2中的一种方法 [英] Java two varargs in one method

查看:119
本文介绍了Java的可变参数2中的一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Java中,创建一个方法,它需要两个不同的可变参数?
我知道,与同类型的对象,因为编译器一点儿也不知道从哪里开始或结束它是不可能的。但是为什么它也有不同的对象类型is'nt可能吗?

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 know where to start or to end. But why it also is'nt possible with to different Object types?

例如:

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

有什么办法来创建这样的方法?
谢谢!

Is there any way to create method like this? Thank you!

推荐答案

只有一个可变参数,对不起。但使用asList()使得它几乎一样方便:

Only one vararg, sorry. But using asList() makes it almost as convenient:

 public void myMethod(List<Integer> args1, List<Integer> args2) {
   ...
 }

 -----------

 import static java.util.Arrays.asList;
 myMethod(asList(1,2,3), asList(4,5,6));

这篇关于Java的可变参数2中的一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆