Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Arrays setAll() Method

In this tutorial, we will learn about setAll() method of Arrays class in Java. This method sets all the elements specified by the generator. This generator is a function which computes some value as per the definition.

Syntax

This method sets all elements of the specified array, using the provided generator function to compute each element.

static void	setAll(double[] array, IntToDoubleFunction generator)

List of Overloading Methods of setAll() Method

This table contains all the overloaded variants of setAll() method.

Method Description

static void setAll(double[] array, IntToDoubleFunction generator)

This method sets all elements of the specified array, using the provided generator function to compute each element.

static void setAll(int[] array, IntUnaryOperator generator)

This method sets all elements of the specified array, using the provided generator function to compute each element.

static void setAll(long[] array, IntToLongFunction generator)

This method sets all elements of the specified array, using the provided generator function to compute each element.

static <T> void setAll(T[] array, IntFunction<? extends T> generator)

This method sets all elements of the specified array, using the provided generator function to compute each element.

Example: The setAll() Method

In the example below, we created an array of size 10. After the creation of the array, we set the value of each array element by using a generator function that will generate a square of an index number. Finally, we can verify it by seeing the output of our code.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int arr[] = new int[10];
		Arrays.setAll(arr, (index)-> index*index);
		for(int num:arr)
		{
			System.out.print(num+" ");
		}
	} 
}


0 1 4 9 16 25 36 49 64 81

Example of all overloading methods of setAll() Method

In the following example, we implemented all the overloading methods of setAll() method. All the prototypes are already discussed above. We can see all the generated arrays in the output by different generators. For this example, we are using a generator that will multiply index*index and set that number for the element of the current index.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static void setAll(int[] array, IntUnaryOperator generator)
		int array1[] = new int[10];
		Arrays.setAll(array1, (index)-> index*index);
		System.out.println(Arrays.toString(array1));
		
		//Example of static void setAll(long[] array, IntToLongFunction generator)
		long array2[] = new long[10];
		Arrays.setAll(array2, (index)-> index*index);
		System.out.println(Arrays.toString(array2));
		
		//Example of static void setAll(double[] array, IntToDoubleFunction generator)
		double array3[] = new double[10];
		Arrays.setAll(array3, (index)-> index*index);
		System.out.println(Arrays.toString(array3));
	}
}


[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0]

Conclusion

In this tutorial, we learned how to use setAll() method to set values to an array element. This method sets all elements of the specified array, using the provided generator function to compute each element.