Close

Java Collections - Arrays.setAll() Examples

Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods set all elements of the specified array, using the provided generator function to compute each element.

public static <T> void setAll(T[] array,
                              IntFunction<? extends T> generator)
public static void setAll(int[] array,
                          IntUnaryOperator generator)
public static void setAll(long[] array,
                          IntToLongFunction generator)
public static void setAll(double[] array,
                          IntToDoubleFunction generator)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class SetAllExample {

public static void main(String... args) {
int[] arr = new int[10];
Arrays.setAll(arr, (index) -> 1 + index);
System.out.println(Arrays.toString(arr));
}
}

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]




package com.logicbig.example.arrays;

import java.util.Arrays;

public class SetAllExample2 {

public static void main(String... args) {
String[] arr = new String[10];
Arrays.parallelSetAll(arr, (index) -> Integer.toString(index + 1));
System.out.println(Arrays.toString(arr));
}
}

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]




See Also