- Trending Categories
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
Java Program to fill elements in an int array
Elements can be filled in an int array using the java.util.Arrays.fill() method. This method assigns the required int value to the int array in Java. The two parameters required are the array name and the value that is to be stored in the array elements.
A program that demonstrates this is given as follows −
Example
import java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { int[] intArray = new int[5]; int intValue = 9; Arrays.fill(intArray, intValue); System.out.println("The int array content is: " + Arrays.toString(intArray)); } }
Output
The int array content is: [9, 9, 9, 9, 9]
Now let us understand the above program.
First the int array intArray[] is defined. Then the value 9 is filled in the int array using the Arrays.fill() method. Finally, the int array is printed using the Arrays.toString() method. A code snippet which demonstrates this is as follows −
int[] intArray = new int[5]; int intValue = 9; Arrays.fill(intArray, intValue); System.out.println("The int array content is: " + Arrays.toString(intArray));
- Related Articles
- Fill elements in a Java int array in a specified range
- Java Program to fill elements in a float array
- Java Program to fill elements in a short array
- Java Program to fill elements in a char array
- Java Program to fill elements in a long array
- Java Program to fill elements in a byte array
- Java Program to fill an array with random numbers
- Java Program to convert int array to IntStream
- Java Program to write int array to a file
- Java Program to Print the Elements of an Array
- Java Program to fill an array of characters from user input
- What does the method fill(int[], int fromIndex, int toIndex, int val) do in java?
- What does the method fill(int[], int val) do in java?
- Java Program to convert an int value to String
- How do I reverse an int array in Java

Advertisements