- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fill array values in Java?
Let us first create an int array −
int[] arr = new int[10];
Now, fill array values. Here, the numbers get added from the index 2 to 7 −
Arrays.fill(arr, 2, 7, 100);
Example
import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] arr = new int[10]; System.out.println("Array = "+Arrays.toString(arr)); Arrays.fill(arr, 2, 7, 100); System.out.println("Fill = "+Arrays.toString(arr)); } }
Output
Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Fill = [0, 0, 100, 100, 100, 100, 100, 0, 0, 0]
Advertisements