Arrays.fill(machines, 0);
时间: 2024-06-05 12:09:04 浏览: 7
这行代码的作用是将数组 machines 中的所有元素都赋值为 0。Arrays.fill() 方法的用法是 Arrays.fill(array, value),其中 array 是要填充的数组,value 是要填充的值。这个方法可以快速地将一个数组中的所有元素都赋值为同一个值。在这个例子中,将数组 machines 中的所有元素都赋值为 0,可以初始化这个数组,使得所有机器的状态都为“空闲”。
相关问题
Arrays.fill
Arrays.fill is a method in Java that is used to fill an array with a specific value. It is a convenient way to initialize an array to a specific value or reset the contents of an array to a default value.
The method takes three arguments: the array to be filled, the starting index from which to fill the array, and the ending index up to which the array should be filled. The syntax for the method is as follows:
Arrays.fill(array, startIndex, endIndex, value);
The startIndex is the index at which to start filling the array and the endIndex is the index up to which the array should be filled. The value parameter specifies the value with which to fill the array.
Here's an example of using the Arrays.fill method to initialize an array with a specific value:
int[] myArray = new int[5];
Arrays.fill(myArray, 0, 5, 10);
This code creates a new int array with 5 elements, and then fills it with the value 10 starting from index 0 and ending at index 4.
Note that if the startIndex is greater than or equal to the endIndex, the method will not modify the array. Also, if the endIndex is greater than the length of the array, an ArrayIndexOutOfBoundsException will be thrown.
arrays.fill方法
`Arrays.fill()`是Java中的一个静态方法,用于将指定数组中的每个元素都设置为指定的值。它接受三个参数:数组、起始索引和结束索引(不包括)以及要设置的值。
语法:
```
public static void fill(int[] a, int val)
public static void fill(int[] a, int from, int to, int val)
public static void fill(boolean[] a, boolean val)
public static void fill(boolean[] a, int from, int to, boolean val)
public static void fill(byte[] a, byte val)
public static void fill(byte[] a, int from, int to, byte val)
public static void fill(char[] a, char val)
public static void fill(char[] a, int from, int to, char val)
public static void fill(double[] a, double val)
public static void fill(double[] a, int from, int to, double val)
public static void fill(float[] a, float val)
public static void fill(float[] a, int from, int to, float val)
public static void fill(long[] a, long val)
public static void fill(long[] a, int from, int to, long val)
public static void fill(short[] a, short val)
public static void fill(short[] a, int from, int to, short val)
```
其中,`a`是要填充的数组,`val`是要填充的值,`from`是起始索引,`to`是结束索引(不包括)。
示例:
```
int[] arr = new int[5];
Arrays.fill(arr, 5);
System.out.println(Arrays.toString(arr));
// 输出 [5, 5, 5, 5, 5]
int[] arr2 = new int[10];
Arrays.fill(arr2, 2, 7, 8);
System.out.println(Arrays.toString(arr2));
// 输出 [0, 0, 8, 8, 8, 8, 2, 2, 2, 2]
```
在第一个示例中,我们将一个长度为5的整型数组填充为值5。在第二个示例中,我们将一个长度为10的整型数组中从索引2到索引7(不包括)的元素填充为值8。