将列表作为varargs传入
问题内容:
我有一个List<Thing>
,我想将其传递给声明的方法doIt(final Thing... things)
。有没有办法做到这一点?
代码看起来 事情 是这样的:
public doIt(final Thing... things)
{
// things get done here
}
List<Thing> things = /* initialized with all my things */;
doIt(things);
该代码显然是行不通的,因为doIt()
需要Thing
不List<Thing>
。
有没有一种方法可以将列表作为变量传递?
这是在Android App中,但我不明白为什么该解决方案不适用于Java
问题答案:
刚过去things.toArray(new Thing[things.size()])
。