需求:定义一个方法fill(int[] arr,int fromIndex,int toIndex,int value)
功能:将数组arr中的元素从索引fromIndex开始到toIndex(不包含toIndex)对应的值改为value
public static void main(String[] args) {
int [] a = {12,23,45,55,63,70,77,88,85,90};
fill(a, 5, 8, 5);
System.out.print("[");
for (int i = 0; i < a.length; i++) {
if(i==a.length-1) {
System.out.print(a[i]);
}else {
System.out.print(a[i]+",");
}
}
System.out.print("]");
}
public static void fill(int[] arr,int fromIndex,int toIndex,int value) {
for (int i =fromIndex; i < toIndex; i++) {
arr[i]=value;
}
}
输出结果为:
[12,23,45,55,63,5,5,5,85,90]