Code subject: method with a variable number of parameters
Posted: Fri Dec 12, 2008 9:56 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
In java J2SE0.5 you can now make a function with a variable number of paramters. The rules to do that is :
- Only varaible paramter list only . - varaible paramter list must apper in the last inside the parentheses of the function. - defined as : type followed by three dots and the name .
And example about method with a variable number of parameters.
Code:
import java.io.*; class MyClass { public void printStuff(String greet, int... values) { for (int v : values ) { System.out.println( greet + ":" + v); } } } class VarargTest { public static void main(String[] args) { MyClass mc = new MyClass(); mc.printStuff("Hello", 1); mc.printStuff("Hey", 1,2); mc.printStuff("Hey you", 1,2,3); } }
/* code taken from apress SCJP 1.5 */
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )