1

I am using an API which requires me to use the below function:

out.writeNext(trtd[0], trtd[1], trtd[2]);

As you may guess from the code, the array elements can only be separated by comma. I may know the length of the trtd array but how do I dynamically add more elements as arguments? So that the code can be varied for n elements (as we can't do a for loop here when arguments vary and need separation by comma).

out.writeNext(trtd[0], trtd[1], trtd[2],.........,trtd[n]);

I thought of getting the arguments in a string and then converting that to code but I tried different methods didn't work for me. Please help me with answers.

7
  • 1
    What API is it that could possibly have this restriction? Could you not simply call writeNext for each element of the array? Commented Oct 1, 2015 at 20:31
  • No it would perform some other operation. I need to take the arguments together and the Library I am trying to use is OpenCSV Commented Oct 1, 2015 at 20:32
  • 3
    Are you sure you can't just pass the array itself? The method signature looks close to a Varargs signature, which can accept either a comma-delimited list of elements, or an actual array. Commented Oct 1, 2015 at 20:33
  • Can't you use a for loop and add with the index. Commented Oct 1, 2015 at 20:35
  • I cannot pass the array itself. Commented Oct 1, 2015 at 20:37

1 Answer 1

2

"writeNext" method is like at the below(Link);

public void writeNext(java.lang.String... nextLine)

So you can pass your all parameter via;

out.writeNext(trtd);

Because "trtd" is string array. "someMethod(String... str)" means you can pass parameters as "someMethod("a", "b", "c");" or you can pass string array as; String [] arr= {"a", "b", "c"}; -> "someMethod(arr);"

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works. I hope I don't get such problem where I can't use loops like above. For API, yes this is perfect answer. Thanks!

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.