Arrays.stream(double[], int, int): This method is available in java.util.Arrays class of Java.
Syntax:
DoubleStream java.util.Arrays.stream(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 sequential DoubleStream with the specified range of the specified array as its source.
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 DoubleStream for the array range.
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 Arraysstreamdoublerange {public static void main(String[] args) {double array[] = { 1, 2, 10, 5, 7, 8, 9 };int startInclusive = 1, endExclusive = 4;System.out.println(Arrays.stream(array,startInclusive, endExclusive).count());}}
Output:
3
Approach 2: ArrayIndexOutOfBoundsException
Java
import java.util.Arrays;public class Arraysstreamdoublerange {public static void main(String[] args) {double array[] = { 1, 2, 10, 5, 7, 8, 9 };int startInclusive = -1, endExclusive = 4;System.out.println(Arrays.stream(array,startInclusive, endExclusive).count());}}
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) at java.base/java.util.Arrays.stream(Arrays.java:5536)
No comments:
Post a Comment