Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamic method parameters

 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
or
System.out.println( arg + arg + arg )
where it is undeterminable how many argument you will be accepting.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your question is in your question itself:
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
someMethod(arg1)
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
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll will try to make this more clear.

[ May 25, 2002: Message edited by: Trent Green ]
 
Junilu Lacar
Bartender
Pie
Posts: 7793
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()});
Junilu
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course C# supports that.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://www.jcp.org/jsr/detail/65.jsp
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic