简体   繁体   中英

Java: Invoke a method with an variable length argument lists with an ArrayList with arbitrary length

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM