Arrays.parallelPrefix(int[], int, int, IntBinaryOperator) in Java

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

Syntax:

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

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

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

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

        int fromIndex = 2, toIndex = 4;
        IntBinaryOperator 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.IntBinaryOperator;

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

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

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



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

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

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

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



Approach 4: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

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

        int array[] = null;

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



No comments:

Post a Comment