Java Array class fill(byte[] a, int fromIndex, int toIndex, byte val) example
Array class fill(byte[] a, int fromIndex, int toIndex, byte val) method example
Array class fill(byte[] a, int fromIndex, int toIndex, byte val) method example. This example shows you how to use fill(byte[] a, int fromIndex, int toIndex, byte val)method.This method Assigns the specified byte value to each element of the specified range of the specified array of bytes.
Here is the code:-
/**
* @Program that Assigns the specified byte value to each element of the
* specified range of the specified array of bytes.
* fill3Array.java
* Author:-RoseIndia Team
* Date:-16-May-2008
*/
import java.util.*;
public class fill3Array {
public static void main(String[] args) {
//creating a byte array
byte b[] = {1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 5, 4, 3, 2};
System.out.print("The original array is:-");
for (int i = 0; i < b.length; i++) //Display the byte array
{
System.out.print(b[i]);
}
System.out.println();
//Assigns the specified byte value to each element of the specified range of
//the specified array
Arrays.fill(b, 2, 12, (byte) 9);
System.out.print("The Changed array is:-");
for (int i = 0; i < b.length; i++) //Display the Changed byte array
{
System.out.print(b[i]);
}
}
} |
Output of the program:-
The original array is:-12333333333334565432
The Changed array is:-12999999999934565432 |
|