22
33import java .util .stream .IntStream ;
44
5- // Sample uses of varargs
5+ // Sample uses of varargs (Pages 245-6)
66public class Varargs {
7-
8- // Simple use of varargs - Page 245
7+ // Simple use of varargs (Page 245)
98 static int sum (int ... args ) {
109 int sum = 0 ;
1110 for (int arg : args )
1211 sum += arg ;
1312 return sum ;
1413 }
1514
16- // Simple use of varargs - Page 197
17- static int sum2 (int ... args ) {
18- return IntStream .of (args ).sum ();
19- }
20-
21- // // The WRONG way to use varargs to pass one or more arguments! - Page 245
15+ // // The WRONG way to use varargs to pass one or more arguments! (Page 245)
2216// static int min(int... args) {
2317// if (args.length == 0)
2418// throw new IllegalArgumentException("Too few arguments");
@@ -29,7 +23,7 @@ static int sum2(int... args) {
2923// return min;
3024// }
3125
32- // The right way to use varargs to pass one or more arguments - Page 246
26+ // The right way to use varargs to pass one or more arguments ( Page 246)
3327 static int min (int firstArg , int ... remainingArgs ) {
3428 int min = firstArg ;
3529 for (int arg : remainingArgs )
@@ -38,7 +32,6 @@ static int min(int firstArg, int... remainingArgs) {
3832 return min ;
3933 }
4034
41-
4235 public static void main (String [] args ) {
4336 System .out .println (sum (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ));
4437 System .out .println (min (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ));
0 commit comments