gpt4 book ai didi

java - 将列表作为可变参数传递

转载 作者:IT老高 更新时间:2023-10-28 13:24:24 25 4
gpt4 key购买 nike

我有一个 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 应用程序中,但我不明白为什么该解决方案不适用于任何 Java

最佳答案

只需传递 things.toArray(new Thing[things.size()]).

关于java - 将列表作为可变参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12220324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com