Java example on java.util.Arrays.fill() method

This tutorial explains how to fill array with a specified value.To fill an array with specified value in java use Arrays.fill() method.fill() method takes two inputs array and value.

Arrays.fill() Signature :

static void fill(byte []a,byte val)
static void fill(char []a,char val)
static void fill(double []a,double val)
static void fill(float []a,float val)
static void fill(int []a,int val)
static void fill(long []a,long val)
static void fill(short []a,short val)
static void fill(Object []a,Object val)
static void fill(byte []a,byte val)

java.util.Arrays.fill() Example

import java.util.Arrays;

public class ArraysFillDemo {

public static void main(String[] args) {

		Integer array1[]={1,2,3,4,5};

		Arrays.fill(array1, 5);

		for (Integer integer : array1) {
			System.out.println(integer);
		}

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *