Total members 11894 |It is currently Mon Nov 25, 2024 5:18 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Methods with Variable Arguments List :

With java 5, you can have a function with variable number of arguments (called for short: var-args) , you do this following the rules below :
1.Specify the type of arguments (can be primitive or Object).
2.Use the (…) syntax.
3. You can have other arguments with var-arg .
4.You can have only one var-arg .
5.The var-arg must be last on in method arguments.

See the following example :

Code:
public class VariableArg {
    public static void main(String  args[])
    {
        int output = sum(1,2,4,5,1,3);
        System.out.println("Sum = "+output);
    }

    public static int sum(int ... x)
    {
        int result =0 ;
        for (int i=0;i<x.length;i++)
        {
             result+=x[i];
        }
        return result;
    }

}





The output is :

Code:
Sum = 16




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

This program is awesome....



Author:
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : methods with variable arguments list
 different between Local variable and Global variable     -  
 Which methods is best for SEO?     -  
 What Are Constructor Methods?     -  
 JSP Passing Arrays to Methods     -  
 Static Methods and Variables     -  
 methods in ActionListener interface     -  
 Calling Overloaded Methods     -  
 how to run inidividual test methods in junit4     -  
 Can we override static methods in java     -  
 Program to compute commission using two methods     -  



Topic Tags

Java Methods






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com