Forums Register Login
dynamic method parameters
Is there a way to define a method to accept as many arguments as posible.
I am not talking about overloading either.
I thinking more on the lines like the main method
public static void main(String[] args)
System.out.println( arg + arg + arg )
where it is undeterminable how many argument you will be accepting.
The answer to your question is in your question itself:
I know that you can pass an array, but i dont want the user to have to make an array in order to pass a list of argument to my method.
I want it to be something like
the println method
where one call to the method might be
and some other call to the method might be
someMethod( arg1, args2 args3 )
i dont want them to have to make and array of arguments and then pass my the reference to that array
I'll will try to make this more clear.

[ May 25, 2002: Message edited by: Trent Green ]
There's no way to do this in Java. You have to either accept an array or write many overloaded versions of your method. If you study it more closely, you'll realize that the all the overloaded versions of println() method really take only one parameter. It just seems that it allows many parameters because of the way the + operator is overloaded for Strings.
It's not really that much of a bother to pass an array to a method. Consider these examples:
foo(new int[] {1, 2, 3, 4, 5});
foo(new Object[] {"A String", new Object(), new java.util.ArrayList()});
Of course C# supports that.
See http://www.jcp.org/jsr/detail/65.jsp
Thank you very much Ilja Preuss. The link provided above was very informative. Thanks to all the other "Rancher Hands" too.
I really love this site. It is one of the few special places on the net.
[ May 25, 2002: Message edited by: Trent Green ]
Popeye has his spinach. I have this tiny ad:
Why should you try IntelliJ IDEA ?

This thread has been viewed 687 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 20, 2018 20:23:55.