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.
Code Java:
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);
}
}
Re: How to Pass unlimited Arguments to a Function
Hey neo,
Thanks for this useful example. I moved it to the Tips & Tutorials forum :)
Re: How to Pass unlimited Arguments to a Function
Never mind JavaPF,That's the main purpose form the forum.
Thanks again....