Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Array Fill

Java Example – Array Fill

Java Examples Java Examples

In the following example, we use the Arrays.fill(arrayname, value) method and the Arrays.fill(arrayname, starting index, ending index, value) method of the Java Util class to fill an array with elements:

Main.java File

import

java

.

util

.*;

public

class

FillTest

{

public

static

void

main

(

String

args

[

]

)

{

int

array

[

]

=

new

int

[

6

]

;

Arrays

.

fill

(

array

,

100

)

;

for

(

int

i

=

0

,

n

=

array

.

length

;

i

<

n

;

i

++

)

{

System

.

out

.

println

(

array

[

i

]

)

;

}

System

.

out

.

println

(

)

;

Arrays

.

fill

(

array

,

3

,

6

,

50

)

;

for

(

int

i

=

0

,

n

=

array

.

length

;

i

<

n

;

i

++

)

{

System

.

out

.

println

(

array

[

i

]

)

;

}

}

}

The output of the above code is:



100

100

100

100

100

100



100

100

100

50

50

50

Java Examples Java Examples