Arrays.parallelPrefix(long[], int, int, LongBinaryOperator): This method is available in java.util.Arrays class of Java.
Syntax:
void java.util.Arrays.parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)
This method takes four arguments one of type long array and the other two are of type int and the other one of type LongBinaryOperator as its parameters. This method performs parallelPrefix(long [], LongBinaryOperator)for the given subrange of the array.
Parameters: Four parameters are required for this method.
array: the array.
fromIndex: the index of the first element, inclusive.
toIndex: the index of the last element, exclusive.
op: a side-effect-free, associative function to perform the accumulation
Throws:
1. IllegalArgumentException - if fromIndex > toIndex.
2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > array.length.
3. NullPointerException - if the specified array or function is null.
Approach 1: When no exceptions
Java
import java.util.Arrays;import java.util.function.LongBinaryOperator;public class ArraysparallelPrefixlongrange {public static void main(String[] args) {long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };int fromIndex = 2, toIndex = 4;LongBinaryOperator sum = (d1, d2) -> d1 + d2;Arrays.parallelPrefix(array, fromIndex, toIndex, sum);System.out.println(Arrays.toString(array));}}
Output:
[1, 2, 3, 7, 10, 23, 45, 56, 78]
Approach 2: IllegalArgumentException
Java
import java.util.Arrays;import java.util.function.LongBinaryOperator;public class ArraysparallelPrefixlongrange {public static void main(String[] args) {long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };int fromIndex = 5, toIndex = 4;LongBinaryOperator sum = (d1, d2) -> d1 + d2;Arrays.parallelPrefix(array, fromIndex, toIndex, sum);System.out.println(Arrays.toString(array));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(4) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)
Approach 3: ArrayIndexOutOfBoundsException
Java
import java.util.Arrays;import java.util.function.LongBinaryOperator;public class ArraysparallelPrefixlongrange {public static void main(String[] args) {long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };int fromIndex = -1, toIndex = 4;LongBinaryOperator sum = (d1, d2) -> d1 + d2;Arrays.parallelPrefix(array, fromIndex, toIndex, sum);System.out.println(Arrays.toString(array));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)
Approach 4: NullPointerException
Java
import java.util.Arrays;import java.util.function.LongBinaryOperator;public class ArraysparallelPrefixlongrange {public static void main(String[] args) {long array[] = null;int fromIndex = 2, toIndex = 4;LongBinaryOperator sum = (d1, d2) -> d1 + d2;Arrays.parallelPrefix(array, fromIndex, toIndex, sum);System.out.println(Arrays.toString(array));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is null at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)
No comments:
Post a Comment