Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 2:01 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Code Snippet
 Code subject: methods with variable arguments list
PostPosted: Sat Aug 22, 2009 4:15 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time

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

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: methods with variable arguments list
PostPosted: Wed Nov 30, 2011 6:21 am 
This program is awesome....


TOP
  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Which methods is best for SEO?  Google  Gabrial  5
 Calling Overloaded Methods  Java  Anonymous  0
 can u check tis code to perform union & intersection of list  C-C++  Anonymous  1
 How to write a PHP coding to list out all files and director  PHP  Andrrew  2
 fill selection options from list  ASP/ASP.net examples  msi_333  0

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team