Array fill value
In this chapter you will learn:
Fill value to array
The following methods assigns the specified value to each element of the specified array.
static void fill(boolean[] a, boolean val)
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
static void fill(byte[] a, byte val)
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
static void fill(char[] a, char val)
static void fill(char[] a, int fromIndex, int toIndex, char val)
static void fill(double[] a, double val)
static void fill(double[] a, int fromIndex, int toIndex, double val)
static void fill(float[] a, float val)
static void fill(float[] a, int fromIndex, int toIndex, float val)
static void fill(int[] a, int val)
static void fill(int[] a, int fromIndex, int toIndex, int val)
static void fill(long[] a, int fromIndex, int toIndex, long val)
static void fill(long[] a, long val)
static void fill(Object[] a, int fromIndex, int toIndex, Object val)
static void fill(Object[] a, Object val)
static void fill(short[] a, int fromIndex, int toIndex, short val)
static void fill(short[] a, short val)
The following code fills an array of int.
import java.util.Arrays;
// j a v a 2 s .com
public class Main{
public static void main(String args[]) {
int array[] = new int[10];
for (int i = 0; i < 10; i++){
array[i] = -3 * i;
}
System.out.print("Original contents: ");
System.out.println(Arrays.toString(array));
// fill and display
Arrays.fill(array, 2, 6, -1);
System.out.print("After fill(): ");
System.out.println(Arrays.toString(array));
}
}
The output:
Fill an array of boolean value
import java.util.Arrays;
/* j a va2s . co m*/
public class Main {
public static void main(String[] argv) throws Exception {
boolean[] booleanArr = new boolean[10];
boolean booleanFillValue = false;
Arrays.fill(booleanArr, booleanFillValue);
}
}
import java.util.Arrays;
/*from jav a 2 s . c o m*/
public class Main {
public static void main(String[] argv) throws Exception {
int startIndex = 0;
int endIndex = 4;
boolean[] booleanArr = new boolean[10];
boolean booleanFillValue = true;
Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);
System.out.println(Arrays.toString(booleanArr));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Array