Arrays.spliterator(double[], int, int) in Java

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

Syntax:

OfDouble java.util.Arrays.spliterator(double[] array, int startInclusive, int endExclusive)

This method takes three arguments one of type double array and the rest two are of type int as its parameters. This method returns a Spliterator.OfDouble covering the specified range of the specified array.

Parameters: Three parameters are required for this method.

array: the array, assumed to be unmodified during use.

startInclusive: the first index to cover, inclusive.

endExclusive: index immediately past the last index to cover.

Returns: a spliterator for the array elements.

Throws:

1. ArrayIndexOutOfBoundsException - if startInclusive is negative, endExclusive is less than startInclusive, or endExclusive is greater than the array size.

Approach 1: When no exceptions

Java

import java.util.Arrays;

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

        double array[] = { 1, 2, 10, 5 };

        int startInclusive = 0;
        int endInclusive = 2;

        System.out.println(Arrays.spliterator(array,
startInclusive, endInclusive));
    }
}

Output:

java.util.Spliterators$DoubleArraySpliterator@39ed3c8d


Approach 2: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

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

        double array[] = { 1, 2, 10, 5 };

        int startInclusive = -1;
        int endInclusive = 4;

        System.out.println(Arrays.spliterator(array,
startInclusive, endInclusive));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Spliterators.checkFromToBounds(Spliterators.java:391) at java.base/java.util.Spliterators.spliterator(Spliterators.java:371) at java.base/java.util.Arrays.spliterator(Arrays.java:5413)


No comments:

Post a Comment