[Java] 纯文本查看 复制代码 import java.util.ArrayList;
/*
* 定义一个replaceAll方法,将传入的新值替换集合中的老值(list,old,new)
*/
public class Practice6 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("b");
list.add("f");
System.out.println(list);
replaceAll(list,"b","bb");
System.out.println(list);
}
private static void replaceAll(ArrayList<String> list, String string, String string2) {
for(int i = 0; i < list.size(); i++){
if(string.equals(list.get(i))){
list.set(i, string2);
}
}
}
}
|