Java Utility Library

Java Arrays - spliterator() Method



The java.util.Arrays.spliterator() method returns a Spliterator covering the specified range of the specified array.

Syntax

static <T> Spliterator<T> spliterator(T[] array,
                                      int startInclusive,
                                      int endExclusive)

Here, T is the type of elements in the array.


Parameters

array Specify the array, assumed to be unmodified during use.
startInclusive Specify the first index to cover, inclusive.
endExclusive Specify the index immediately past the last index to cover.

Return Value

Returns a spliterator for the array elements.

Exception

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

Example:

In the example below, the java.util.Arrays.spliterator() method returns a spliterator with elements in specified range of the given array object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Long array
    Long[] MyArr = {10l, 2l, -3l, 35l, 56l};

    //creating spliterator object on Array 
    Spliterator<Long> splitr = Arrays.spliterator(MyArr, 1, 4); 

    //printing estimateSize of the Array
    System.out.println("Estimated size: " + splitr.estimateSize());  

    //display content of the Array using 
    //tryAdvance method
    System.out.print("The Array contains: ");              
    splitr.forEachRemaining((n) -> System.out.print(n + " "));    
  }
}

The output of the above code will be:

Estimated size: 3
The Array contains: 2 -3 35 

❮ Java.util - Arrays