How to fill array values in Java?


Let us first create an int array −

int[] arr = new int[10];

Now, fill array values. Here, the numbers get added from the index 2 to 7 −

Arrays.fill(arr, 2, 7, 100);

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      int[] arr = new int[10];
      System.out.println("Array = "+Arrays.toString(arr));
      Arrays.fill(arr, 2, 7, 100);
      System.out.println("Fill = "+Arrays.toString(arr));
   }
}

Output

Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Fill = [0, 0, 100, 100, 100, 100, 100, 0, 0, 0]

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

893 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements