This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

method with an unknown number of parameters

 
Ranch Hand
Posts: 327
  • 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 ]
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Ranch Hand
Posts: 149
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic