source:
doc/theses/rob_schluntz_MMath/examples/intro/variadic.java@
d0a00a5a
| Last change on this file since d0a00a5a was 67982887, checked in by , 7 years ago | |
|---|---|
|
|
| File size: 508 bytes | |
| Rev | Line | |
|---|---|---|
| [9c14ae9] | 1 | class variadic { |
| 2 | int sum(int... args) { | |
| 3 | int s = 0; | |
| 4 | for (int x : args) { | |
| 5 | s += x; | |
| 6 | } | |
| 7 | print(args.length, " ", args[0], " ", args[args.length-1], "\n"); | |
| 8 | return s; | |
| 9 | } | |
| 10 | ||
| 11 | void print(Object... objs) { | |
| 12 | for (Object obj : objs) { | |
| 13 | System.out.print(obj); | |
| 14 | } | |
| 15 | } | |
| 16 | ||
| 17 | public void run() { | |
| 18 | print("The sum from 1 to 10 is ", sum(1,2,3,4,5,6,7,8,9,10), ".\n"); | |
| 19 | print(sum(new int[]{1, 2,3}), "\n"); | |
| 20 | } | |
| 21 | ||
| 22 | public static void main(String args[]) { | |
| 23 | new variadic().run(); | |
| 24 | } | |
| 25 | } |
Note:
See TracBrowser
for help on using the repository browser.