How to Fill Array with Default Values in Java


In this example we will show how to fill an array with default values.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class FillArray {

	public static void main(String[] args) {
		// Create empty array.
		String[] arr = new String[10];
		
		/*
		 * Use the method Arrays.fill() 
		 * to fill an empty array with default values.
		 */
		Arrays.fill(arr, "X");
		for(String str : arr) {
			System.out.println(str);
		}
	}

}

Output:

X
X
X
X
X
X
X
X
X
X

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments