We're a place where coders share, stay up-to-date and grow their careers.
Java 8:
int[] array = { 1, 2, 3, 4, 5 }; final int product = Arrays.stream(array) .reduce((x,y) -> x * y ) .getAsInt(); int[] newArray = Arrays.stream(array) .map(x -> product / x) .toArray(); System.out.println(Arrays.toString(newArray));
Fails for elements with a 0, obviously, could be worked around with filtering.
Java 8:
Fails for elements with a 0, obviously, could be worked around with filtering.