在指定范围内将元素填充到Java字节数组中
可以使用java.util.Arrays.fill()方法将元素填充到指定范围内的Java字节数组中。此方法将指定范围内的所需字节值分配给Java中的字节数组。
Arrays.fill()方法所需的参数是数组名称,要填充的第一个元素的索引(包括),要填充的最后一个元素的索引(包括)以及要存储在其中的值数组元素。
演示此的程序如下所示-
示例
import java.util.Arrays;
public class Demo {
public static void main(String[] argv) throws Exception {
byte[] byteArray = new byte[10];
byte byteValue = 2;
int indexStart = 3;
int indexFinish = 6;
Arrays.fill(byteArray, indexStart, indexFinish, byteValue);
System.out.println("The byte array content is: " + Arrays.toString(byteArray));
}
}输出结果
The byte array content is: [0, 0, 0, 2, 2, 2, 0, 0, 0, 0]
现在让我们了解上面的程序。
首先定义字节数组byteArray[]。然后,使用Arrays.fill()方法以从索引3(含)到索引6(不含)的值2填充字节数组。最后,使用Arrays.toString()方法打印字节数组。演示这的代码片段如下-
byte[] byteArray = new byte[10];
byte byteValue = 2;
int indexStart = 3;
int indexFinish = 6;
Arrays.fill(byteArray, indexStart, indexFinish, byteValue);
System.out.println("The byte array content is: " + Arrays.toString(byteArray));热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短