Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

fill()

Example

Assign the value “Kiwi” to all elements in the array.

String[] fruits = {"Banana", "Orange", "Apple", "Mango"};
Arrays.fill(fruits, "Kiwi");

Definition and Usage

The fill() method populates an array with a designated value.

Note: The value must match the data type of the array.

Tip: You can specify start and end positions; otherwise, all elements will be filled.

Syntax

Arrays.fill(array, value)

Arrays.fill(array, start, end, value)

Parameter Values

Parameter

Description

array

Necessary: Rephrase the array intended for population.

start

Voluntary: Rephrase the starting index position of the first element (inclusive) to be populated.

end

Voluntary: Rephrase the ending index position of the last element (exclusive) to be populated.

value

Necessary: Rephrase the value intended for populating the array.

Technical Details

Returns:

No return value

Java version:

1.2 (java.util)

More Examples

Example

Populate the final two elements.

String[] fruits = {"Banana", "Orange", "Apple", "Mango"};
Arrays.fill(fruits, 2, 4, "Kiwi");