ADVERTISEMENT
ADVERTISEMENT

Java - Example of Variable Length Argument List.


IncludeHelp 02 July 2016

Java Variable Length Argument List - in this technique we can define a method having variable length argument list of variables.

In this code snippet we will declare a method with variable length argument list, method name will be sum and we will pass 2, 3, 4, 5 and 6 arguments.

Java Code Snippet - Example of Variable Length Argument List

//Java - Example of Variable Length Argument List. 
 
class VariableLengthArgList
{
	public static int sumOfNumbers(int... num){
		int sum=0;
		
		for(int n:num){
			sum+=n;
		}
		return sum;
	}
	
	public static void main(String args[])
	{
		System.out.println("SUM 1: "+ sumOfNumbers(10,20));
		System.out.println("SUM 2: "+ sumOfNumbers(10,20,30));
		System.out.println("SUM 3: "+ sumOfNumbers(10,20,30,40));
		System.out.println("SUM 4: "+ sumOfNumbers(10,20,30,40,50));
		System.out.println("SUM 5: "+ sumOfNumbers(10,20,30,40,50,60));
		
	}
}
    
    SUM 1: 30
    SUM 2: 60
    SUM 3: 100
    SUM 4: 150
    SUM 5: 210

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.