i got a function with the following signature
foo(Object...){}
And i got an ArrayList "list"
How can i invoke the function with an ArrayList of arbitrary length. i mean how can i avoid this
if(list.size == 1){
foo(list.get(0))
}else if(list.size == 2){
foo(list.get(0),list.get(1))
}
您可以在ArrayList
上调用toArray()
将ArrayList
传递给varargs方法foo
。
foo(list.toArray());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.