让我们首先创建一个int数组-
int[] arr = new int[10];
现在,填充数组值。在这里,数字从索引2到7相加-
Arrays.fill(arr, 2, 7, 100);
import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] arr = new int[10]; System.out.println("Array = "+Arrays.toString(arr)); Arrays.fill(arr, 2, 7, 100); System.out.println("Fill = "+Arrays.toString(arr)); } }
输出结果
Array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Fill = [0, 0, 100, 100, 100, 100, 100, 0, 0, 0]