Java variable number of arguments for a method

JavaMethodsArgumentsParameter PassingVariadic Functions

Java Problem Overview


Is it possible to declare a method that will allow a variable number of parameters ?

What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters?

Answer: varargs

Java Solutions


Solution 1 - Java

That's correct. You can find more about it in the Oracle guide on varargs.

Here's an example:

void foo(String... args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}

which can be called as

foo("foo"); // Single arg.
foo("foo", "bar"); // Multiple args.
foo("foo", "bar", "lol"); // Don't matter how many!
foo(new String[] { "foo", "bar" }); // Arrays are also accepted.
foo(); // And even no args.

Solution 2 - Java

Yes, it's possible:

public void myMethod(int... numbers) { /* your code */ }

Solution 3 - Java

Variable number of arguments

It is possible to pass a variable number of arguments to a method. However, there are some restrictions:

  • The variable number of parameters must all be the same type
  • They are treated as an array within the method
  • They must be the last parameter of the method

To understand these restrictions, consider the method, in the following code snippet, used to return the largest integer in a list of integers:

private static int largest(int... numbers) {
     int currentLargest = numbers[0];
     for (int number : numbers) {
        if (number > currentLargest) {
            currentLargest = number;
        }
     }
     return currentLargest;
}

source Oracle Certified Associate Java SE 7 Programmer Study Guide 2012

Solution 4 - Java

For different types of arguments, there is 3-dots :

public void foo(Object... x) {
    String myVar1  = x.length > 0 ? (String)x[0]  : "Hello";
    int myVar2     = x.length > 1 ? Integer.parseInt((String) x[1]) : 888;
} 

Then call it

foo("Hii"); 
foo("Hii", 146); 

for security, use like this:
if (!(x[0] instanceof String)) { throw new IllegalArgumentException("..."); }

> The main drawback of this approach is that if optional parameters are of different types you lose static type checking. Please, see more variations .

Solution 5 - Java

Solution 6 - Java

Yes Java allows vargs in method parameter .

public class  Varargs
{
   public int add(int... numbers)
   int result = 1for(int number: numbers)
      {
         result= result+number;  
      }  return result; 
   }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser69514View Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaDolphView Answer on Stackoverflow
Solution 3 - JavaAbey EllaView Answer on Stackoverflow
Solution 4 - JavaT.ToduaView Answer on Stackoverflow
Solution 5 - JavaRoland BoumanView Answer on Stackoverflow
Solution 6 - JavaCode_Eat_SleepView Answer on Stackoverflow