How to add new element to Varargs?
Asked Answered
L

8

13

I have a method

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

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);

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.

Leakey answered 4/7, 2012 at 2:46 Comment(1)
Possible duplicate of How to call a varargs method with an additional argument from a varargs methodEnsample
T
2

extra is just a String array. As such:

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

You may need to mess with the generic type of extrasList.toArray().

You can be faster but more verbose:

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);
Tonietonight answered 4/7, 2012 at 2:51 Comment(5)
I get Cannot invoke asList() on the array type String[] for the first option.Leakey
Did you mean to use Arrays.asList(extra)? (see Radek's comment).Cacique
Now I get Cannot invoke toArray() on the primitive type boolean for Arrays.asList(extra).add(Description).toArray()Leakey
Will given an ArrayIndexOutOfBoundsException.Ensample
This will no longer work (tested with Java 8u144) since Arrays.asList() returns an fixed-size List. Suggest to use Arrays.copyOf(...).Aachen
D
13

To expand on some of the other answers here, the array copy could be done a bit faster with

String[] newArr = new String[extra.length + 1];
System.arraycopy(extra, 0, newArr, 0, extra.length);
newArr[extra.length] = Description;
Deraign answered 4/7, 2012 at 3:1 Comment(0)
A
5

Use Arrays.copyOf(...) :

String[] extra2 = Arrays.copyOf(extra, extra.length+1);
extra2[extra.length] = description;

object_for_text = getObject(find_arguments,extra2);
Aachen answered 4/10, 2017 at 9:58 Comment(0)
T
2

extra is just a String array. As such:

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

You may need to mess with the generic type of extrasList.toArray().

You can be faster but more verbose:

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);
Tonietonight answered 4/7, 2012 at 2:51 Comment(5)
I get Cannot invoke asList() on the array type String[] for the first option.Leakey
Did you mean to use Arrays.asList(extra)? (see Radek's comment).Cacique
Now I get Cannot invoke toArray() on the primitive type boolean for Arrays.asList(extra).add(Description).toArray()Leakey
Will given an ArrayIndexOutOfBoundsException.Ensample
This will no longer work (tested with Java 8u144) since Arrays.asList() returns an fixed-size List. Suggest to use Arrays.copyOf(...).Aachen
E
1

Do you mean something like this?

public boolean findANDsetText(String description, String ... extra)
{
    String[] newArr = new String[extra.length + 1];
    int counter = 0;
    for(String s : extra) newArr[counter++] = s;
    newArr[counter] = description;

    // ...

    Foo object_for_text = getObject(find_arguments, newArr);

    // ...
}
Egghead answered 4/7, 2012 at 2:52 Comment(0)
R
0

Its simply this way...

Treat the Var-args as below...

Example:

In your above example the 2nd parameter is "String... extra"

So you can use like this:

extra[0] = "Vivek";
extra[1] = "Hello";

Or

for (int i=0 ; i<extra.length ; i++)

  {

          extra[i] = value;

  }
Radices answered 4/7, 2012 at 2:58 Comment(0)
C
0

With Java 11 use as parameter for a new List:

List<String> templateArguments = new ArrayList<(Arrays.asList(args));
templateArguments.add(throwable.getMessage());
String.format(template, templateArguments.toArray());
Constrictor answered 3/12, 2019 at 19:20 Comment(0)
K
0

Conversion to list and back to array, but shorter using utility functions:

// import com.google.common.collect.Lists;

var descriptionAndExtra
    = Lists.asList(description, extra).toArray(new String[extra.length + 1]));
Kira answered 10/2, 2020 at 15:4 Comment(0)
D
0

Assuming you want description to be the first element in new array you can do the following since 8:

Stream.concat(Stream.of(description), Stream.of(extra)).toArray(String[]::new)
Debris answered 13/6 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.