
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
Fill Elements in a Byte Array in Java
Elements can be filled in a byte array using the java.util.Arrays.fill() method. This method assigns the required byte value to the byte 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 { byte[] byteArray = new byte[5]; byte byteValue = 5; Arrays.fill(byteArray, byteValue); System.out.println("The byte array content is: " + Arrays.toString(byteArray)); } }
Output
The byte array content is: [5, 5, 5, 5, 5]
Now let us understand the above program.
First the byte array byteArray[] is defined. Then the value 5 is filled in the byte array using the Arrays.fill() method. Finally, the byte array is printed using the Arrays.toString() method. A code snippet which demonstrates this is as follows −
byte[] byteArray = new byte[5]; byte byteValue = 5; Arrays.fill(byteArray, byteValue); System.out.println("The byte array content is: " + Arrays.toString(byteArray));
Advertisements