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