Java – how to add a new element to varargs?

I have a way

public boolean findANDsetText  (String Description,String ... extra ) {@H_404_3@ 
 

在里面我想调用另一个方法并传递额外的东西,但我想添加新的元素(描述)到临时演员.

object_for_text = getObject(find_arguments,extra);@H_404_3@ 
 

我怎么能在java中这样做?代码会是什么样的?

我厌倦了容纳this question的代码,但无法使其工作.

Solution

Extra is just a string array Therefore:

List<String> extrasList = Arrays.asList(extra);
extrasList.add(description);
getObject(find_arguments,extrasList.toArray());@H_404_3@ 
 

您可能需要使用extrasList.toArray()的泛型类型.

你可以更快但更冗长:

String[] extraWithDescription = new String[extra.length + 1];
int i = 0;
for(; i < extra.length; ++i) {
  extraWithDescription[i] = extra[i];
}
extraWithDescription[i] = description;
getObject(find_arguments,extraWithDescription);@H_404_3@
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>