You can do it as follows:
private void sum() {
int a[] = {2, 6, 1, 4};
int b[] = {2, 1, 4, 4};
int result[] = new int[a.length];
Arrays.setAll(result, i -> a[i] + b[i]);
}
First, the int result[]correct size is created .
Then, with the release of Java 8 yesterday, the easy part appeared:
- You can do
Arrays.setAll(int[] array, IntUnaryOperator); IntUnaryOperator lambda, , map i to a[i] + b[i], .Arrays.parallelSetAll