File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method with an unknown number of parameters

 
Joseph Sweet
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to write a method that can get an unknown number of int parameters...
do you know what should be the syntax for that. I know the syntax in C++, do not have a clue what it is in java.

the method should do something really simple, like summing up the numbers it gets. problem is one time I can call it with 5 numbers and the next time with 10 etc.
[ May 07, 2005: Message edited by: Joseph Sweet ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This feature (commonly called "varargs") was introduced in Java 5; it didn't exist previously. Even so, it's actually just synactic sugar for a final argument of array type.

A function sum() that added an arbitrary number of int arguments would look like



And you could then call

sum(3)
sum(3, 4, 5)
sum(3, 4, 5, 6, 7)

etc.
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm varargs, we have that now?

Tzk

Anyway, if you don't use 5 you might do it a little different.

You could pass an Array of ints to a sum function.

J.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic