Java 将 List<String> 传递给 String... 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32223161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:11:13  来源:igfitidea点击:

Passing List<String> to String... parameter

javaandroidarraysvariadic-functions

提问by RuNaWaY87

I'm struggling to pass a List of Strings into a method requiring the parameter "String...".

我正在努力将字符串列表传递到需要参数“字符串...”的方法中。

Can anybody help me out?

有人可以帮我吗?

// How to put names into dummyMethod?
List<String> names = getNames();


 public void dummyMethod(String... parameter) {
    mInnerList.addAll(Arrays.asList(parameter));
}

采纳答案by Glorfindel

You'll have to convert the List<String>to a String array in order to use it in the 'varargs' parameter of dummyMethod. You can use toArraywith an extra array as parameter. Otherwise, the method returns an Object[]and it won't compile:

您必须将 转换List<String>为字符串数组,以便在 的 'varargs' 参数中使用它dummyMethod。您可以使用toArray额外的数组作为参数。否则,该方法将返回一个Object[]并且不会编译:

List<String> names = getNames();
dummyMethod(names.toArray(new String[names.size()]));

回答by Naman Gala

Pass String array (String[]) inside method. You will have to convert your List to Array and then pass it.

String[]在方法中传递 String 数组 ( )。您必须将 List 转换为 Array 然后传递它。

if (names != null) {
    dummyMethod(names.toArray(new String[names.size()])); 
}

回答by SacJn

This is vararg parameter and for this you should pass array. ArrayList won't work. You can rather convert this list to array before passing to the method.

这是 vararg 参数,为此您应该传递数组。ArrayList 不起作用。您可以在传递给方法之前将此列表转换为数组。

String a = new String[names.size];
list.toArray(a)

回答by Ahmad Al-Kurdi

You can do the following :

您可以执行以下操作:

dummyMethod(names.toArray(new String[names.size()]) 

this will convert the list to array

这会将列表转换为数组

回答by vefthym

Since you later parse the parameter as a List, I suggest changing the method to:

由于您稍后将参数解析为列表,因此我建议将方法更改为:

public void dummyMethod(List<String> parameter) {
    mInnerList.addAll(parameter);
}

to avoid the extra costs.

以避免额外的费用。

However, if you want to use this method "as it is", then you should call it like that:

但是,如果您想“按原样”使用此方法,那么您应该这样调用它:

dummyMethod(names.toArray(new String[names.size()]));

as Glorfindel suggests in his answer, since the three dots mean that you can call the method using many Strings, or an array of Strings (see this postfor more details).

正如 Glorfindel 在他的回答中所建议的那样,因为三个点意味着您可以使用许多字符串或字符串数​​组来调用该方法(有关更多详细信息,请参阅此帖子)。

回答by akhil_mittal

The var-arg actually accepts an array and you can use it like:

var-arg 实际上接受一个数组,您可以像这样使用它:

dummyMethod(names.toArray(new String[names.size()]));

Here is a sample code:

这是一个示例代码:

List<String> names = new ArrayList<>();
names.add("A");
names.add("B");
dummyMethod(names.toArray(new String[names.size()]));