如何向Varargs添加新元素? [英] How to add new element to Varargs?
本文介绍了如何向Varargs添加新元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法
public boolean findANDsetText (String Description, String ... extra ) {
我想调用另一种方法并传递它 extras
但是我想要为额外内容添加新元素(描述)。
inside I want to call another method and pass it extras
but I want to add new element (Description) to extras.
object_for_text = getObject(find_arguments,extra);
我怎样才能在java中做到这一点?代码会是什么样的?
How can I do that in java? What would the code look like?
我厌倦了来自这个问题但无法使其发挥作用。
I tired to accommodate code from this question but couldn't make it work.
推荐答案
额外的
只是一个 String
数组。因此:
extra
is just a String
array. As such:
List<String> extrasList = Arrays.asList(extra);
extrasList.add(description);
getObject(find_arguments, extrasList.toArray());
您可能需要弄乱泛型类型 extrasList.toArray()
。
You may need to mess with the generic type of 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);
这篇关于如何向Varargs添加新元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文