路过一个列表,作为可变参数 [英] Passing a List in as varargs

查看:182
本文介绍了路过一个列表,作为可变参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名单,其中,事> ,我想将它传递给声明的方法 doIt方法(最后一件事...的东西)。有没有办法做到这一点?

I have a List<Thing> and I would like to pass it to a method declared doIt(final Thing... things). Is there a way to do that?

在code相的的东西的是这样的:

The code looks something like this:

public doIt(final Thing... things)
{
    // things get done here
}

List<Thing> things = /* initialized with all my things */;

doIt(things);

这code显然是行不通的,因为 doIt方法()需要的事不是列表与LT;东西&GT;

That code obviously doesn't work because doIt() takes Thing not List<Thing>.

有没有办法传递一个List作为可变参数?

Is there a way to pass in a List as the varargs?

这是一个Android应用程序,但我不明白为什么该解决方案将不会适用于任何的Java

推荐答案

只是通过 things.toArray(新的东西[things.size())

这篇关于路过一个列表,作为可变参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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