数组替换指定元素(fill的用法)

废话不多说直接看代码

package com.hisense.smartroad.saf.workplan.service;

import java.util.Arrays;

public class Test {

	public static void main(String[] args) {
		String staffName = "xiaoming";
		String getFeedbackContent = "6666666666";
		String[] progresss = {"xiaoming:1111;","xiaohong:2222;","xiaolan:3333;"};
		for (int i = 0; i < progresss.length; i++) {
			if (progresss[i].contains(staffName)) {
				 Arrays.fill(progresss,i,i+1,staffName +":"+ getFeedbackContent + ";"); 
				 System.out.println(progresss);
			}
		}
		for (int i = 0; i < progresss.length; i++) {
			System.out.println(progresss[i]);
		}
	}
}

语法:fill(int[] a,int fromIndex,Int toIndex,value)。

    a:要进行填充的数组。
    int fromIndex:要使用指定值填充的第一个元素的索引(包括)。
    Int toIndex:要使用指定值填充的最后一个元素的索引(不包括)
    int value:要存储在数组所有元素中的值。

数组定义

格式1:      int [] arr = new int[5];   

arr[0]=1;

arr[1]=2;

arr[3]=3


arr[4]=4;

arr[5]=5;

格式2:   int [] arr = new int[]{1,3,5,7}                            

 格式3: int[]arr = {1,3,5,7};   
 

猜你喜欢

转载自blog.csdn.net/qq_37538698/article/details/83578533
今日推荐