I want to call a function like private void passStrings(String... arg){}
And I have an array which store a set of Strings but don't know its size.
So How can I use all that array value and call the function passString().
Asked
Active
Viewed 207 times
0

Yunyuen Chan
- 135
- 1
- 11
-
use a while loop – OLIVER.KOO Aug 18 '17 at 06:04
-
@OLIVER.KOO: I think you may have missed the point of the question. – Jon Skeet Aug 18 '17 at 06:04
-
I thought the OP wants to call `passStrings()` for each `String` in his array, and that is why OP doesn't know how many time he should call `passStrings()` cause he doesn't know the array size? I could be wrong. (also @JonSkeet nice to meet you. I had to buy your book your book C# in depth two years ago when I was in college.) – OLIVER.KOO Aug 18 '17 at 06:07
-
@OLIVER.KOO: Given that `passStrings` *accepts* an array, I believe they can just call it once. I believe they just weren't aware that they *could* pass an array. – Jon Skeet Aug 18 '17 at 06:12
-
@JonSkeet It works fine. Thanks – Yunyuen Chan Aug 18 '17 at 07:34
1 Answers
1
Just pass it as the array:
String[] array = { "Some", "arguments", "I", "prepared", "earlier" };
passStrings(array);
A varargs parameter like arg
is still an array parameter really - it's just that the compiler allows you to specify the elements individually, if you want. It doesn't force you to though - if you've already got the array, just pass it.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194