Javatpoint Logo
Javatpoint Logo

Java Arrays Fill

By using the Arrays.fill() method, we can either fill a complete array or can fill some part of it. Arrays.fill() method is also capable of filling both the 2D and the 3D Arrays.

Arrays fill() method has the following syntax:

Here,

  1. The arr parameter defines an array to be filled.
  2. The formIndex parameter defines the index of the first element to be filled with the given value.
  3. The toIndex parameter defines the index of the last index to be filled with the given value.
  4. The val parameter defines the value to be stored in all elements of the given array.

Let's take some examples to understand how we can use the Arrays.fill() method for filling array.

Fill entire 1-D Array

ArraysFillExample1.java

Output:

Java Arrays Fill

Fill some part of 1-D Array

ArraysFillExample2.java

Output:

Java Arrays Fill

Fill multidimensional array (2D Array)

Just like a single-dimensional array, we can also fill the multidimensional array by using the Arrays.fill() method. In order to fill a multidimensional array, we use the for loop to fill each row of the multidimensional array.

Let's take an example to understand how we can fill a multidimensional array using the Arrays.fill() method.

ArraysFillExample3.java

Output:

Java Arrays Fill

ArraysFillExample4.java

Output:

Java Arrays Fill
Next TopicJava Font





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA