-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVarargsMethod.java
More file actions
50 lines (43 loc) Β· 1.07 KB
/
Copy pathVarargsMethod.java
File metadata and controls
50 lines (43 loc) Β· 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package day5;
public class VarargsMethod {
/*
varargs can be only one in one function
they must be the last parameter
time complexity: O(1)
space complexity: O(1)
*/
public static void main(String[] args) {
// System.out.println(sum(1, 2, 3, 4, 5));
// System.out.println(sum());
// System.out.println(sum(-100, 90));
func('a');
func('a', 'n', 'i');
func('i', 'j');
}
/*
time complexity: O(n)
space complexity: O(1)
*/
private static int sum(int... numbers) {
int sum = 0;
for (int element : numbers) {
sum += element;
}
return sum;
}
/*
time complexity: O(1)
space complexity: O(1)
*/
private static void func(char normal, char... characters) {
System.out.println(normal);
System.out.println(characters);
}
// a b c d
// i a
// j bcd cd b phi
// k phi d cd bcd
// phi = nothing
private static void func2(char i, char j, char... k) {
}
}