Skip to content

Commit bd75c55

Browse files
committed
finally found a better java implementation in exchange of primitive types
1 parent 476e4cc commit bd75c55

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

FunctionPointerDemo2.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.function.Function;
2+
3+
public class FunctionPointerDemo2 {
4+
static Integer increment(Integer n) {
5+
return n + 1;
6+
}
7+
8+
static void myObjective(Integer[] arr, Function<Integer, Integer> f) {
9+
for (int i = 0; i < arr.length; ++i) {
10+
arr[i] = f.apply(arr[i]);
11+
}
12+
}
13+
14+
public static void main(String[] args) {
15+
Integer[] arr = {0, 1, 2, 3, 4};
16+
myObjective(arr, FunctionPointerDemo2::increment);
17+
18+
for (Integer elem : arr) {
19+
System.out.print(elem + " ");
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)