Arrays.parallelPrefix(double[], int, int, DoubleBinaryOperator) in Java

Arrays.parallelPrefix(double[], int, int, DoubleBinaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)

This method takes four arguments one of type double array, two of type int, and the other one of type DoubleBinaryOperator as its parameters. This method performs parallelPrefix(double [], DoubleBinaryOperator)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.DoubleBinaryOperator;

public class ArraysparallelPrefixdoublerange {
    public static void main(String[] args) {

        double array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 2, toIndex = 4;
        DoubleBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1.0, 2.0, 3.0, 7.0, 10.0, 23.0, 45.0, 56.0, 78.0]


Approach 2: IllegalArgumentException

Java

import java.util.Arrays;
import java.util.function.DoubleBinaryOperator;

public class ArraysparallelPrefixdoublerange {
    public static void main(String[] args) {

        double array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 5, toIndex = 4;
        DoubleBinaryOperator 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:1504)



Approach 3: ArrayIndexOutOfBoundsException

Java

import java.util.Arrays;
import java.util.function.DoubleBinaryOperator;

public class ArraysparallelPrefixdoublerange {
    public static void main(String[] args) {

        double array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = -1, toIndex = 4;
        DoubleBinaryOperator 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:1504)



Approach 4: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.DoubleBinaryOperator;

public class ArraysparallelPrefixdoublerange {
    public static void main(String[] args) {

        double array[] = null;

        int fromIndex = 2, toIndex = 4;
        DoubleBinaryOperator 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:1504)



No comments:

Post a Comment