Home  >  Core Java

Example of Arrays.fill in java

By Arvind Rai, May 18, 2013
Arrays.fill fills an array with a given element. In below code snippet
String[] strArr = new String[5];
Arrays.fill(strArr, "A"); 
The array strArr will be filled by element 5. It means at every index there will be element 5. Find the example.
ArrayFillDemo.java
package com.util;
import java.util.Arrays;
public class ArrayFillDemo {
    public static void main(String[] args) {
        String[] strArr = new String[5];
        Arrays.fill(strArr, "A");
        for (int i = 0; i < strArr.length; i++) {
            System.out.print(strArr[i] + " ");
        }
        Integer[] inrArr = new Integer[5];
        System.out.println();
        Arrays.fill(inrArr, 5);
        for (int i = 0; i < inrArr.length; i++) {
            System.out.print(inrArr[i] + " ");
        }
    }
}
Output
A A A A A
5 5 5 5 5 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us