Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to Pass unlimited Arguments to a Function

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post How to Pass unlimited Arguments to a Function

    This sample demonstrate how to pass unlimited arguments to your function and how to know object types passed to your function.

    public class UnlimitedArgs {
     
        /** Default constructor */
        public UnlimitedArgs() {
        }
     
        public static void main(String args[])
        {
           UnlimitedArgs g = new UnlimitedArgs();
           g.viewObjArgs("Mega_unknown@hotmail.com",1.100,1111,1.2,411,552555.66);
        }
     
        /**
         * Unlimited Argument List like printf in C++,Java .Holds int Type.
         * unlimited int values
         */
        private void viewIntArgs(int ... Numbers){
            for(int i : Numbers)
            {
                System.out.println(i);
            }
            System.out.println("Lenght of my argument list is : " + Numbers.length);
        }
     
        //What if we dont know object type. So we will set data type as Object
        /**
         *Unlimited Argument List like printf in C++,Java .Holds Object Type.
         */
        private void viewObjArgs(Object ... objects)
        {
            for(Object obj:objects)
            {
                //return type name 
                System.out.println(obj.getClass());
            }
            System.out.println("Lenght of my argument list is : " + objects.length);
        }    
    }

  2. The Following User Says Thank You to neo_2010 For This Useful Post:

    JavaPF (July 8th, 2009)


  3. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to Pass unlimited Arguments to a Function

    Hey neo,

    Thanks for this useful example. I moved it to the Tips & Tutorials forum
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #3
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: How to Pass unlimited Arguments to a Function

    Never mind JavaPF,That's the main purpose form the forum.
    Thanks again....

Similar Threads

  1. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM
  2. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  3. Traverse Function for Knights tour
    By budder8818 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 4th, 2009, 03:49 PM