
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What Does the fill Method Do in Java?
The fill(int[] a, int fromIndex, int toIndex, int val) method of the java.util.Arrays class assigns the specified int value to each element of the specified range of the specified array of integers. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive.(If fromIndex==toIndex, the range to be filled is empty.)
Example
import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { int arr[] = new int[] {1, 6, 3, 2, 9}; System.out.println("Actual values: "); for (int value : arr) { System.out.println("Value = " + value); } Arrays.fill(arr, 2, 4, 18); System.out.println("New values after using fill() method: "); for (int value : arr) { System.out.println("Value = " + value); } } }
Output
Actual values: Value = 1 Value = 6 Value = 3 Value = 2 Value = 9 New values after using fill() method: Value = 1 Value = 6 Value = 18 Value = 18 Value = 9
Advertisements