How to initialize an array with Array.fill() method
The blog provides the technical approach on ” How to initialize an array and fill with values using Array.fill () .
package com.java.planforexams.array; import java.util.Arrays; public class ArrayUsingFillMethod { public static void main(String args[]) { int intializedArray[] = new int[5]; // Array filled with value 21 for all indexes Arrays.fill(intializedArray, 21); for (int value : intializedArray) { System.out.println(" \n Current filled Array Value ::: " + value); } System.out.println(" Filling Array with value 20 with start index as 2 and last index as 4"); Arrays.fill(intializedArray, 2, 4, 20); for (int value : intializedArray) { System.out.println(" \n Final filled Array Value ::: " + value); } } } Output::: Current filled Array Value ::: 21 Current filled Array Value ::: 21 Current filled Array Value ::: 21 Current filled Array Value ::: 21 Current filled Array Value ::: 21 Filling Array with value 20 with start index as 2 and last index as 4 Final filled Array Value ::: 21 Final filled Array Value ::: 21 Final filled Array Value ::: 20 Final filled Array Value ::: 20 Final filled Array Value ::: 21
Leave a Reply