In this post i would like show you a cool feature of java to handle functions with different parameter sets. For example if you want to find sum of a set of parameters which maybe 2or 3or400 you can use this feature instead of doing messy convertions to array.
Output:
1
3
6
10
15
21
public class threeDots{
static void sumIt(int...x){
int t=0;
for(int i:x)
t+=i;
System.out.println(t);
}
public static void main(String[] args) {
sumIt(1);
sumIt(1,2);
sumIt(1,2,3);
sumIt(1,2,3,4);
sumIt(1,2,3,4,5);
sumIt(1,2,3,4,5,6);
}
}
Output:
1
3
6
10
15
21
0 comments :
Post a Comment