Pages

Tuesday, August 11, 2020

How To Set All values of Array To Same Value In Faster Way?

1. Overview

In this article, You will learn how to set or fill the all values of the array with a single value in a faster way. This is quite interesting to do but very easy.

In the previous article, I have shown how to create and initialize the array with values.

And also will show the example program to create an array with n copies of the same value/object using Arrays.fill() method.

How To Set All values of Array To Same Value In Faster Way?


2. How To Set All Values Of Array To Same Value

First, Let us write a simple code that creates an int array and fills with the given value.

package com.javaprogramto.arrays.fill;

public class FillArrayExampleCustom {

    public static void main(String[] args) {

        System.out.println("Array 1");        int[] array1 = fillArray(5, 10);
        printArray(array1);

        System.out.println("Array 2"); int[] array2 = fillArray(10, 25); printArray(array2); } private static int[] fillArray(int size, int valueToBeFilled){ // Creating an array int[] intArray = new int[size]; // setting the value to each index in the array for(int i =0;i<intArray.length; i++){ intArray[i] = valueToBeFilled; } return intArray; } private static void printArray(int[] array){ System.out.println("\n"); // printing the array for (int i = 0;i< array.length;i++){ System.out.print(" "+array[i] ); } } }
Output:
Array 1
10 10 10 10 10

Array 2
25 25 25 25 25 25 25 25 25 25
As you see the output, it just assigned the given value to each index by running a simple for loop.

3. Using Arrays.fill() Method


The above method works only for the primitive integer array. For each type, a new method should be created manually.

It is a bit painful to do. For such cases, java Arrays class provides several utility methods to make developer life easy.

Arrays.fill() method takes different types of arguments and fills the whole array with the same value.

fill() method is an overloaded method with all primitive types. Below is the syntax for the int[] array and all other methods follow the same.
public static void fill(int[] a, int val)

public static void fill(int[] a,
        int fromIndex,
        int toIndex,
        int val)
		
		
And also these fill() method works with start index and end index for setting the values. The remaining values do not change.
package com.javaprogramto.arrays.fill;

import java.util.Arrays;

public class ArraysFillExample {

    public static void main(String[] args) {

        int[] array1 = new int[5];
        // filling int values
        Arrays.fill(array1, 9);
        System.out.println(Arrays.toString(array1));

        int[] array2 = new int[10];
        // filling int values from index 3 to 7
        Arrays.fill(array2, 3, 7, 9);
        System.out.println(Arrays.toString(array2));

        boolean[] array3 = new boolean[10];
        // filling boolean values
        Arrays.fill(array3, true);
        System.out.println(Arrays.toString(array3));

        boolean[] array4 = new boolean[10];
        // filling int values from index 5 to 9
        Arrays.fill(array4, 5,9,true);
        System.out.println(Arrays.toString(array4));

        float[] array5 = new float[10];
        // filling float values
        Arrays.fill(array5, 55.5f);
        System.out.println(Arrays.toString(array5));

        float[] array6 = new float[10];
        // filling int values from index 2 to 7
        Arrays.fill(array6, 2,7,77.7f);
        System.out.println(Arrays.toString(array6));

    }
}

	
Output:
[9, 9, 9, 9, 9]
[0, 0, 0, 9, 9, 9, 9, 0, 0, 0]
[true, true, true, true, true, true, true, true, true, true]
[false, false, false, false, false, true, true, true, true, false]
[55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5, 55.5]
[0.0, 0.0, 77.7, 77.7, 77.7, 77.7, 77.7, 0.0, 0.0, 0.0]

4. Arrays.fill() Method Internals


Look at the below fill() method internal code that runs a simple for loop to set values.

Both our own custom logic and Arrays.fill() method works in same in terms of performance and same fast.
public static void fill(char[] a, char val) {
        for (int i = 0, len = a.length; i < len; i++)
            a[i] = val;
    }
	
public static void fill(boolean[] a, int fromIndex, int toIndex,
                            boolean val) {
        rangeCheck(a.length, fromIndex, toIndex);
        for (int i = fromIndex; i < toIndex; i++)
            a[i] = val;
    }
	
	public static void fill(double[] a, double val) {
	        for (int i = 0, len = a.length; i < len; i++)
	            a[i] = val;
	    }		

5. Conclusion


In this article, You've seen how to fill the whole array with one value using the utility method and Arrays.fill() method.

Arrays.fill() method supports to fill from a particular start index to end index.

Examples shown in this article are over GitHub.


No comments:

Post a Comment

Please do not add any spam links in the comments section.