是否可以将List作为参数发布到Java中具有无限参数(...)的函数?

时间:2015-07-14 03:18:21

标签: java variadic-functions

假设我有一个第三方库,它公开了一个接受无限参数的函数:

void foo(Bar ... bar);

我无法更改此库或为该库创建新函数,我只能使用它们所暴露的内容。接下来,我有一个要使用的参数列表:

List<Bar> bars = new ArrayList<Bar>();
// list initialization by some complex algorithm.

我希望能够将此列表用作foo函数的参数。我知道我可以这样做:

for (Bar bar: bars)
    foo(bar);

但我需要尽可能减少对foo的调用次数,所以我试图立即发布一个完整的列表。这样的事情可以用Java吗?

1 个答案:

答案 0 :(得分:4)

因为它是一个可变参数(或varargs)参数,是的;它可以与数组互换。你只需要创建数组。

使用List#toArray(T[] arr),这很简单:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Do not automatically add a slash for existing directories
DirectorySlash Off


#No trailing slash. This means that an URL entered with trailing / will change to an URL without trailing /
#The !-d rule can be included so that any URLs to existing directories are not changed
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

#Allow URL's which do not have the php file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^  %{REQUEST_FILENAME}.php [L]


#Redirect any file which does not exist to the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [R=301,L]

</IfModule>