Fill all values in an array
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Arrays.fill(arr, 1);
for (int v: arr) {
System.out.println(v);
}
}
}
1
1
1
1
1
1
1
1
1
1
Fill range in an array
import java.util.Arrays;
public class Main2 {
public static void main(String[] args) {
int[] arr = new int[10];
Arrays.fill(arr, 1);
Arrays.fill(arr, 0, 5, 2); // fill first five values with 2
for (int v: arr) {
System.out.println(v);
}
}
}
2
2
2
2
2
1
1
1
1
1