• Post Reply Bookmark Topic Watch Topic
  • New Topic

java variable argument question  RSS feed

 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In java 1.5+ we can use the variable arguments like this:


I wonder, can we form the argument then calling the function, passing along the formed argument?
Say if some condition my arguments would be "hello", else would be "hello" and "world".

I couldn't find anything related to this use so my bet is that this is not possible. Can we use a String array and pass it?

Thanks,
Ben
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand your question. Could you please give any more detail? Thanks!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use a String array and pass it?


Sure. In fact, that is how it is done under the covers. The method sees that input as an array of parameters. And on the calling side an array is created and sent. You can do the same thing i-- with the array syntax, of course.

Henry
 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Daly wrote:I don't quite understand your question. Could you please give any more detail? Thanks!


Yes I know it was not clear as I like it to be, partly because I don't know how to do it, if at all possible.

Basically all the examples I see are like this, you define a function with the ... syntax, like (String ... values). When you use the function, the examples I found are all like "static", meaning they are given arguments like func ( "a"), func ("a", "b" ) and func ("a", "b", "c").

But what I would like to have is to dynamically generate the arguments. I'd rather not try to write specifically if they are "a", or "a", "b", or anything else.

Thanks,
Nianqing
 
Ben Zhang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Can we use a String array and pass it?


Sure. In fact, that is how it is done under the covers. The method sees that input as an array of parameters. And on the calling side an array is created and sent. You can do the same thing i-- with the array syntax, of course.

Henry


OK I will try this. Thanks Henry and Eric.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!