1

I am calling a library method on a fluent API that takes a var args of Strings. when I call it I always pass at least three strings and then a few more depending on circumstance.

Can have a final static array or something to capture the three Strings that are always passed?

3
  • Do u want to transform ArrayList<String> to String[]? Commented Aug 10, 2019 at 22:10
  • What API and method are you using? Commented Aug 10, 2019 at 22:13
  • 1
    Just wrap this method into your own that always pass these 3 values in addition to the other ones, received as argument. Commented Aug 10, 2019 at 22:14

2 Answers 2

3

Perhaps something like this:

public class Overloaded {

  private static final String[] CONST_ARR = {"1", "2", "3"};

  public static void main(String[] args) {
    Overloaded o = new Overloaded();

    o.withConstantStrings();

    String[] a = {"1", "2"};

    o.withAdditionalStrings(a);
  }

  public void withConstantStrings() {
    libraryVarArgsCode(CONST_ARR);
  }

  public void withAdditionalStrings(String... additional) {
    String[] join = new String[additional.length + CONST_ARR.length];

    System.arraycopy(CONST_ARR, 0, join, 0, CONST_ARR.length);
    System.arraycopy(additional, 0, join, CONST_ARR.length, additional.length);

    libraryVarArgsCode(join);
  }

  public void libraryVarArgsCode(String... args) {
    // librabry code here
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here is an idea:

static final String[] RESERVED = {"A", "B", "C"};

...

libMethod(Stream.concat(Arrays.stream(RESERVED), Stream.of("D")).toArray(String[]::new));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.